动态调用WebService

1. 调用代码

 1 string cdfURL = "http://****/test.asmx";
 2             string webserviceName = cdfURL.ToString().Substring(cdfURL.ToString().LastIndexOf("/") + 1, (cdfURL.ToString().LastIndexOf(".") - cdfURL.ToString().LastIndexOf("/")) - 1);
 3             System.Reflection.Assembly asm = EasyTMS.Helper.WebServiceHelp.GetServiceAssembly(cdfURL.ToString());//获取WebService程序集                         
 4             Type stype = asm.GetType(webserviceName, true, true);
 5             object sObj = Activator.CreateInstance(stype);
 6             object[] para = new object[2];
 7             para[0] = "参数1";
 8             para[1] = "参数2";
 9             System.Reflection.MethodInfo mi = stype.GetMethod("Upload");//调用方法
10             object rtn = mi.Invoke(sObj, para);
View Code


2. WebServiceHelp

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 public class WebServiceHelp
 7     {
 8         public static System.Reflection.Assembly GetServiceAssembly(string url)
 9         {
10             // 1. 使用 WebClient 下载 WSDL 信息。
11             System.Net.WebClient web = new System.Net.WebClient();           
12             System.IO.Stream stream = web.OpenRead(url + "?WSDL");
13 
14             // 2. 创建和格式化 WSDL 文档。
15             System.Web.Services.Description.ServiceDescription description = System.Web.Services.Description.ServiceDescription.Read(stream);
16 
17             // 3. 创建客户端代理代理类。
18             System.Web.Services.Description.ServiceDescriptionImporter importer = new System.Web.Services.Description.ServiceDescriptionImporter();
19 
20             importer.ProtocolName = "Soap"; // 指定访问协议。
21             importer.Style = System.Web.Services.Description.ServiceDescriptionImportStyle.Client; // 生成客户端代理。
22             importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties | System.Xml.Serialization.CodeGenerationOptions.GenerateNewAsync;
23             
24             importer.AddServiceDescription(description, null, null); // 添加 WSDL 文档。
25 
26             // 4. 使用 CodeDom 编译客户端代理类。
27             System.CodeDom.CodeNamespace nmspace = new System.CodeDom.CodeNamespace(); // 为代理类添加命名空间,缺省为全局空间。
28             System.CodeDom.CodeCompileUnit unit = new System.CodeDom.CodeCompileUnit();
29             unit.Namespaces.Add(nmspace);
30 
31             System.Web.Services.Description.ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
32             System.CodeDom.Compiler.CodeDomProvider provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
33 
34             System.CodeDom.Compiler.CompilerParameters parameter = new System.CodeDom.Compiler.CompilerParameters();
35             parameter.GenerateExecutable = false;
36             parameter.GenerateInMemory = true;
37             parameter.ReferencedAssemblies.Add("System.dll");
38             parameter.ReferencedAssemblies.Add("System.XML.dll");
39             parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
40             parameter.ReferencedAssemblies.Add("System.Data.dll");
41 
42             System.CodeDom.Compiler.CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
43             System.Reflection.Assembly asm = null;
44             // 5. 使用 Reflection 调用 WebService。
45             if (!result.Errors.HasErrors)
46             {
47                 asm = result.CompiledAssembly;
48             }
49             return asm;
50         }
51 
52     }
View Code
原文地址:https://www.cnblogs.com/kkwoo/p/3488758.html