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.
After talking about this, we actually need to implement this function:
Publicstaticobject 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
// Dynamically call the web Service
Publicstaticobject InvokeWebService (string url, string methodname, object [] args)
{
Return WebServiceHelper. InvokeWebService (url, null, methodname, args );
}
Publicstaticobject 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 );
}
Thrownew Exception (sb. ToString ());
}
// Generate a proxy instance and call the Method
System. Reflection. Assembly 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)
{
Thrownew Exception (ex. InnerException. Message, new Exception (ex. InnerException. StackTrace ));
}
}
Privatestaticstring GetWsClassName (string wsUrl)
{
String [] parts = wsUrl. Split ('/');
String [] pps = parts [parts. Length-1]. Split ('.');
Return pps [0];
}
# Endregion
The above comment has been a good description of the functions of the code segments, the following example to see, this example is to access the http://www.webservicex.net/globalweather.asmx service to obtain the weather conditions of the major cities.
String url = "http://www.webservicex.net/globalweather.asmx ";
String [] args = newstring [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 the XML document, it can be used to analyze weather conditions such as temperature and wind power.
Finally, although C # is still a static language, its dynamic capabilities are also very powerful. 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.