Generally, when WebService needs to be called in a program, we use "add web reference" to let the Vs. NET environment generate a Service proxy for us and then call the corresponding web service. This makes the work simple, but it is bound with the URL, method name, and parameter of the web service. This is the limitation that vs. Net automatically generates Web Service proxy for us. If the URL of the web service has changed on which day, we need to re-Let vs. Net generate the proxy and re-compile it. In some cases, this may be intolerable. We need the ability to dynamically call WebService. For example, you can save the Web Service URL in the configuration file. When the service URL changes, you only need to modify the configuration file.
Webservicehelper class:
- Using system;
- Using system. Web. Services. description;
- Using system. IO;
- Using system. net;
- Using system. text;
- Using system. codedom;
- Using system. codedom. compiler;
- Using Microsoft. CSHARP;
- /// <Summary>
- /// Summary of webservicehelper
- /// </Summary>
- Public class webservicehelper
- {
- Private webservicehelper ()
- {
- }
- // Dynamically call the Web Service
- Public static object invokewebservice (string URL, string methodname, object [] ARGs)
- {
- Return webservicehelper. invokewebservice (URL, null, methodname, argS );
- }
- Public static object invokewebservice (string URL, string classname, string methodname, object [] ARGs)
- {
- String @ namespace = "enterpriseserverbase. WebService. dynamicwebcalling ";
- If (classname = NULL) | (classname = ""))
- {
- Classname = webservicehelper. getwsclassname (URL );
- }
- Try
- {
- // Obtain the WSDL
- WebClient WC = new WebClient ();
- Stream stream = WC. openread (URL + "? WSDL ");
- Servicedescription SD = servicedescription. Read (Stream );
- Servicedescriptionimporter SDI = new servicedescriptionimporter ();
- SDI. addservicedescription (SD ,"","");
- Codenamespace Cn = new codenamespace (@ namespace );
- // Generate client proxy code
- Codecompileunit CCU = new codecompileunit ();
- CCU. namespaces. Add (CN );
- SDI. Import (CN, CCU );
- Csharpcodeprovider CSC = new csharpcodeprovider ();
- Icodecompiler ICC = CSC. createcompiler ();
- // Set compilation Parameters
- Compilerparameters cplist = new compilerparameters ();
- Cplist. generateexecutable = false;
- Cplist. generateinmemory = true;
- Cplist. referencedassemblies. Add ("system. dll ");
- Cplist. referencedassemblies. Add ("system. xml. dll ");
- Cplist. referencedassemblies. Add ("system. Web. Services. dll ");
- Cplist. referencedassemblies. Add ("system. Data. dll ");
- // Compile the proxy class
- Compilerresults Cr = ICC. compileassemblyfromdom (cplist, CCU );
- If (true = Cr. errors. haserrors)
- {
- System. Text. stringbuilder sb = new system. Text. stringbuilder ();
- Foreach (system. codedom. compiler. compilererror Ce IN Cr. Errors)
- {
- SB. append (Ce. tostring ());
- SB. append (system. environment. newline );
- }
- Throw new exception (sb. tostring ());
- }
- // Generate a proxy instance and call the Method
- System. reflection. Assembly = Cr. compiledassembly;
- Type T = assembly. GetType (@ namespace + "." + classname, true, true );
- Object OBJ = activator. createinstance (t );
- System. reflection. methodinfo MI = T. getmethod (methodname );
- Return mi. Invoke (OBJ, argS );
- }
- Catch (exception ex)
- {
- Throw new exception (ex. innerexception. Message, new exception (ex. innerexception. stacktrace ));
- }
- }
- Private Static string getwsclassname (string wsurl)
- {
- String [] parts = wsurl. Split ('/');
- String [] PPS = parts [parts. Length-1]. Split ('.');
- Return PPS [0];
- }
- }
Procedure:
- Using system;
- Using system. Web;
- Using system. Web. UI;
- Using system. Web. UI. htmlcontrols;
- Using system. Web. UI. webcontrols;
- Using system. Web. UI. webcontrols. webparts;
- Public partial class test3: system. Web. UI. Page
- {
- Protected void page_load (Object sender, eventargs E)
- {
- String [] ARGs = new string [1];
- ARGs [0] = "202.102.224.68 ";
- Object OBJ = webservicehelper. invokewebservice ("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx", "getcountrycitybyip", argS );
- Response. Write (string []) (OBJ) [1]);
- }
- }
The program has passed the test. If there are other cases, you can modify it to suit your needs.