usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Web.Services.Description;usingsystem.codedom;usingMicrosoft.csharp;usingSystem.CodeDom.Compiler;usingSystem.IO;namespaceIBO. Xjmyqp. utility{ Public classWebserviceinvoke { Public Static ObjectInvokewebservice (stringUrlstringmethodname) { returnInvokewebservice (""UrlNULL, MethodName,NULL); } Public Static ObjectInvokewebservice (stringUrlstringMethodNameObject[] args) { returnInvokewebservice (""UrlNULL, MethodName, args); } Public Static ObjectInvokewebservice (stringNameSpace,stringUrlstringMethodNameObject[] args) { returnInvokewebservice (nameSpace, URL,NULL, MethodName, args); } Public Static ObjectInvokewebservice (stringNameSpace,stringUrlstringClassNamestringMethodNameObject[] args) { string@namespace =NameSpace; if(ClassName = =NULL) || (ClassName = ="") ) {classname=getwsclassname (URL); } Try { //get WSDLWebClient WC =NewWebClient (); Stream Stream= WC. OpenRead (URL +"? WSDL"); ServiceDescription SD=Servicedescription.read (stream); 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 =icc.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(Exception ex) {Throw NewException (ex. Innerexception.message,NewException (ex. Innerexception.stacktrace)); } } Private Static stringGetwsclassname (stringWsurl) { string[] parts = Wsurl.split ('/'); string[] pps = Parts[parts. Length-1]. Split ('.'); returnpps[0]; } }}
View Code
Call:
" Getservicestatus ");
Other Blogs:
http://linglong117.blog.163.com/blog/static/277145472009127514463/
http://blog.csdn.net/gdjlc/article/details/8560405
Dynamic Call WebService