NormallyProgramWhen WebService needs to be called, 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.
After talking about this, we actually need to implement this function:
Public Static Object Invokewebservice ( String URL, String Methodname, Object [] ARGs)
Specifically, URL is the address of the web service, methodname is the name of the service method to be called, argS is the parameter required to call the Web Service, and the returned value is the result returned by the web service.
To implement such a function, you need the following skills: Reflection, codedom, and C # compiler and WebService for programming. With this knowledge, you can easily implement dynamic web service calls:
# Region Invokewebservice
// Dynamic Web service call
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
{
// Get 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 classCode
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 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 proxy instance and call 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 ( ' . ' );
ReturnPPS [0];
}
# Endregion
The above comments provide a good description of the functions of each code segment. The following is an example.Http://www.webservicex.net/globalweather.asmxService to obtain the weather conditions of major cities.
String URL = " Http://www.webservicex.net/globalweather.asmx " ;
String [] ARGs = New String [ 2 ];
ARGs [ 0 ] = This . Textbox_cityname.text;
ARGs [ 1 ] = " China " ;
Object Result = Webservicehelper. invokewebservice (URL, " Getweather " , ArgS );
This . Label_result.text = Result. tostring ();
in the preceding example, two parameters are used to call the web service. The first parameter is the city name, the second parameter is the country name, and the Web service returns an XML document, it can be used to analyze weather conditions such as temperature and wind power.
finally, although C # is still a static language, it also has powerful dynamic capabilities. If you don't believe it, you can look at spring.net's AOP implementation, this kind of "non-intrusive" AOP implementation is more common than that in general. net declarative AOP implementation (usually through AOP attribute) is much more beautiful.