1. Call directly
Known WebService path, you can add a service reference directly-advanced-add a Web reference to enter the WebService URL directly. This is more common and simple.
That is, the complete WebService file directory, as shown,
You can also publish a new webservice in local IIS according to the WebService file directory, and then the program dynamically calls, modifies the URL
Public new string Url {set; get;}
2. generate WebService. cs file and generate DLL C # call based on WSDL file
Sometimes there are not so many files, only WSDL files
The WSDL file can be provided by someone else or obtained from a WebService address:
http://localhost:8888/WS.asmx?wsdl
Visual Studio 2013->visual Studio tools->vs2013 Developer Command Prompt
Command Line Input
WSDL E:\WS.wsdl /out:WS.cs
E:\WS.wsdlis the WSDL file storage path, or can be http://localhost:8888/WS.asmx?wsdl no error, see the path Program Files\Microsoft Visual Studio 12.0\ws.cs has been automatically generated . cs file See function declarations, structs, etc. are very convenient then the command line executes
csc/t:library WS.cs Under the same path, the WS.dll is generated and copied out into the project folder, and the C # WinForm program can also add references.
3.C # dynamic Call WebService
In a C # program, to invoke WebService, it is generally implemented by adding a Web reference. But what if this WebService URL is available during the program's run? Then it must be "dynamic" to call this webservice.
Take an example of using it:
Object New Object [1]; Args. SetValue ("cyy_js"0);
DataTable dt = Webservicehelper.invokewebservice ("http://192.168.0.10/DBMS_CYY/DBMS_Service.asmx "getusertreelistdata" as DataTable;
Yes, it's a bit of a hassle, which means that every time I have to organize the parameters of the function I want to call into a object[], and each call to Invokewebservice is a very inefficient process of creating a dynamic assembly in memory. The second method is definitely not directly using the "instance name. Method Name (parameter list)" to be comfortable. I put it in a class called WebServiceHelper.cs.
Using system.io;using system.web.services.description;using microsoft.csharp;using system.codedom.compiler;using system.codedom; <summary>///Dynamic Call WebService///</summary>//<param name= "url" >webs Ervice address </param>//<param name= "ClassName" > class name </param>//<param name= "Methodna Me > method Name (module name) </param>//<param name= "args" > Parameter list </param>///<RETURNS>OBJEC t</returns> public static object Invokewebservice (string url, string classname, String methodname, object[ ] args) {string @namespace = "ServiceBase.WebService.DynamicWebLoad"; if (classname = = NULL | | classname = = "") {classname = Webservicehelper.getclassname (URL); }//Get Service Description Language (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 the client proxy class code CodeCompileUnit CCU = new CodeCompileUnit (); Ccu. Namespaces.add (CN); Sdi. Import (CN, CCU); CSharpCodeProvider csc = new CSharpCodeProvider (); ICodeCompiler ICC = csc. CreateCompiler (); Sets the compiler's 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"); Compiling proxy classes CompilerResults cr = Icc.compileassemblyfromdom (cplist, CCU); if (true = = Cr. errors.haserrors) {System.Text.StringBuilder sb = new StringBuilder (); foreach (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 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); } private static string GetClassName (string url) {string[] parts = URL. Split ('/'); string[] pps = parts[parts. LengtH-1]. Split ('. '); return pps[0]; }
Reference http://blog.csdn.net/chuxiamuxiang/article/details/5731988
C # 3 Ways to invoke WebService: Call directly, generate WebService. cs files based on WSDL and generate DLL C # calls, dynamic calls