WebServices in C # code that will turn into Dll, implement method invocation

At work often encountered calling WebServices under different circumstances, I most also encounter this situation, the site find a lot of information to achieve a normal use, the code now share with you:

1, the first head Using the above procedure at:

1 using System.CodeDom.Compiler;
2 using System.Web.Services.Description;
3 using System.CodeDom;
4 using System.Reflection;

 

2, create a method of packaging WebServices dll

. 1          Private  BOOL CreateWebServicesDll ()
 2          {
 . 3              // Get the executable file in the current directory 
. 4              String path = AppDomain.CurrentDomain.BaseDirectory + " MyWebServices.dll " ;
 . 5              IF (the File.Exists (path) == to false ) // if there is no file folder is created 
. 6              {
 . 7                  File.Delete (path);
 . 8              }
 . 9              the try 
10              {
 . 11                  // 1. use WebClient downloaded WSDL information. 
12 is                  the WebClient Web = new newWebClient ();
 13                  Stream Stream = web.OpenRead ( " http://192.168.100.101:8090/PrintService/MaterialService.asmx?wsdl " );
 14                  // 2. Create and format a WSDL document. 
15                  a ServiceDescription Description = ServiceDescription.Read (Stream);
 16                  // 3. Create client proxy proxy class. 
. 17                  the ServiceDescriptionImporter Importer = new new the ServiceDescriptionImporter ();
 18 is                  importer.ProtocolName = " Soap " ; // specified access protocol. 
19                 = ServiceDescriptionImportStyle.Client importer.Style; // generate the client proxy. 
20                  importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
 21  
22                  importer.AddServiceDescription (the Description, null , null ); // add a WSDL document.
23  
24-                  // 4. Use CodeDom compile the client proxy class. 
25                  a CodeNamespace nmspace = new new a CodeNamespace (); // add namespace proxy class, the default global space. 
26 is                  a CodeCompileUnit Unit = new new CodeCompileUnit();
27                 unit.Namespaces.Add(nmspace);
28 
29                 ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
30                 CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
31 
32                 CompilerParameters parameter = new CompilerParameters();
33                 parameter.GenerateExecutable = false;
34                 parameter.OutputAssembly = "MyWebServices.dll"; // 指定你所需的任何文件名。
35                 parameter.ReferencedAssemblies.Add("System.dll");
36                 parameter.ReferencedAssemblies.Add("System.XML.dll");
37                 parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
38                 parameter.ReferencedAssemblies.Add("System.Data.dll");
39 
40                 CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
41                 if (result.Errors.HasErrors)
42                 {
 43                      // displays error message compiler     
44 is                      return  to false ;
 45                  }
 46 is                 
47              }
 48             the catch (Exception EX)
 49              {
 50                  return  to false ;
 51 is              }
 52 is  
53 is              return  to true ;
 54 is          }

Note that the code MyWebServices.dll this file is the name we want to generate dll file, you can customize the meeting, Url address needs at the end of WebServices address followed by the? WSDL

3. Use reflection call

. 1          public  String CallWebservices ()
 2          {
 . 3              // directory of the current file Exc 
. 4              String path = AppDomain.CurrentDomain.BaseDirectory + " MyWebServices.dll " ;
 . 5              IF (Directory.Exists (path) == to false ) // if there is no file folder is created 
. 6              {
 . 7                  CreateWebServicesDll ();
 . 8              }
 . 9  
10              Assembly ASM = null ;
 . 11              the Type T = null ;
 12 is  
13 is             the Assembly.LoadFrom = ASM ( " MyWebServices.dll " );
 14              T = asm.GetType ( " MaterialSupplierService " );
 15  
16              Object O = the Activator.CreateInstance (T);
 . 17              the MethodInfo Method t.GetMethod = ( " GeSnIdListByBigBox " ); // method name needs to call the WebServices 
18 is              Object [] args = new new  Object [] { " B1900420193 " };   // WebServices required transmission parameters 
. 19              String result = method.Invoke(o, args) as String;
20 
21             return result;
22         }

 

Guess you like

Origin www.cnblogs.com/billy1688/p/11286626.html