Encapsulation of this class is for later use WebService without adding various references.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Linq;usingSystem.texusingSystem.Net;usingSystem.IO;usingSystem.Web.Services.Description;usingsystem.codedom;usingMicrosoft.csharp;usingSystem.CodeDom.Compiler;classwebserviceagent {/// <summary> ///Instantiate WebServices/// </summary> /// <param name= "url" >webservices Address</param> /// <param name= "methodname" >method to invoke</param> /// <param name= "args" >Put the parameters needed in the webservices in order in this object[]</param> Public Static ObjectInvokewebservice (stringUrlstringMethodNameObject[] args) { //the namespace here is the namespace of the webservices to be referenced, which I have not changed or can use. You can also add a parameter from the outside to pass in. string@namespace ="Client"; Try { //get WSDLWebClient WC =NewWebClient (); Stream Stream= WC. OpenRead (URL +"? WSDL"); ServiceDescription SD=Servicedescription.read (stream); stringclassname = sd. services[0]. Name; ServiceDescriptionImporter SDI=NewServiceDescriptionImporter (); Sdi. Addservicedescription (SD,"",""); CodeNamespace cn=NewCodeNamespace (@namespace); //Generate the client proxy class codeCodeCompileUnit CCU =NewCodeCompileUnit (); Ccu. Namespaces.add (CN); Sdi. Import (CN, CCU); CSharpCodeProvider csc=NewCSharpCodeProvider (); //ICodeCompiler ICC = csc. CreateCompiler (); //Setting Compilation ParametersCompilerParameters cplist =NewCompilerParameters (); 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"); //Compiling proxy classesCompilerResults CR =csc.compileassemblyfromdom (cplist, CCU); if(true==Cr. errors.haserrors) {System.Text.StringBuilder sb=NewSystem.Text.StringBuilder (); foreach(System.CodeDom.Compiler.CompilerError CEinchCr. Errors) {sb. Append (CE. ToString ()); Sb. Append (System.Environment.NewLine); } Throw NewException (sb.) ToString ()); } //build the proxy instance and call the methodSystem.Reflection.Assembly Assembly =cr.compiledassembly; Type T= assembly. GetType (@namespace +"."+ ClassName,true,true); Objectobj =activator.createinstance (t); System.Reflection.MethodInfo mi=T.getmethod (methodname); returnmi. Invoke (obj, args); } Catch { return NULL; } } }
C # dynamic invocation of WebService classes