ClassName:WebServiceInvoke.cs
ClassCode:
1 using System; 2 using System.CodeDom; 3 using System.CodeDom.Compiler; 4 using System.IO; 5 using System.Linq; 6 using System.Net; 7 using System.Reflection; 8 using System.Text; 9 using System.Web.Services.Description; 10 using Microsoft.CSharp; 11 12 /// <summary> 13 ///动态链接web server 14 /// </summary> 15 public class WebServiceInvoke 16 { 17 public WebServiceInvoke() 18 { 19 // 20 //TODO: 在此处添加构造函数逻辑 21 // 22 } 23 24 /// <summary> 25 /// 调用web 服务 26 /// </summary> 27 /// <param name="url">webServer url</param> 28 /// <param name="className">web Server ClassName</param> 29 /// <param name="methodName">web Server methodName</param> 30 /// <param name="parameters">method parameter</param> 31 /// <returns>retValue</returns> 32 public object InvokeWebService(string url,string className,string methodName,object[] parameters) 33 { 34 //1. 从目标 URL 下载 WSDL 数据。 35 //2. 使用 ServiceDescription 创建和格式化 WSDL 文档文件。 36 //3. 使用 ServiceDescriptionImporter 创建客户端代理类。 37 //4. 使用 CodeDom 动态创建客户端代理类程序集。 38 //5. 利用反射调用相关 WebService 方法。 39 40 string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling"; 41 if (string.IsNullOrEmpty(className)) 42 className = GetClassName(url); 43 44 try 45 { 46 //获取WSDL数据 47 WebClient webClient = new WebClient(); 48 Stream stream= webClient.OpenRead(url + "?WSDL"); 49 ServiceDescription sDescription = ServiceDescription.Read(stream); 50 ServiceDescriptionImporter sDescriptionImporter = new ServiceDescriptionImporter(); 51 sDescriptionImporter.AddServiceDescription(sDescription, "", ""); 52 53 //生成客户端代理类代码 54 CodeNamespace codeNamespace = new CodeNamespace(@namespace); 55 CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); 56 codeCompileUnit.Namespaces.Add(codeNamespace); 57 sDescriptionImporter.Import(codeNamespace, codeCompileUnit); 58 59 //设定编译参数 60 CompilerParameters compilerList = new CompilerParameters 61 { 62 GenerateExecutable=false, 63 GenerateInMemory=true 64 }; 65 compilerList.ReferencedAssemblies.Add("System.dll"); 66 compilerList.ReferencedAssemblies.Add("System.XML.dll"); 67 compilerList.ReferencedAssemblies.Add("System.Web.Services.dll"); 68 compilerList.ReferencedAssemblies.Add("System.Data.dll"); 69 70 //编译代理类 71 CSharpCodeProvider csharpCodeProvider = new CSharpCodeProvider(); 72 CompilerResults compilerResult = csharpCodeProvider.CompileAssemblyFromDom(compilerList, codeCompileUnit); 73 if (compilerResult.Errors.HasErrors == true) 74 { 75 StringBuilder errorSb = new StringBuilder(); 76 foreach (var item in compilerResult.Errors) 77 { 78 errorSb.Append(item.ToString()); 79 errorSb.Append(System.Environment.NewLine); 80 } 81 throw new Exception(errorSb.ToString()); 82 } 83 84 //生成代理实例,调用方法 85 Assembly assembly = compilerResult.CompiledAssembly; 86 Type t = assembly.GetType(@namespace + "." + className, true, true); 87 object obj = Activator.CreateInstance(t); 88 MethodInfo methodInfo = t.GetMethod(methodName); 89 var retValue= methodInfo.Invoke(obj, parameters); 90 return retValue; 91 } 92 catch (Exception ex) 93 { 94 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace)); 95 } 96 } 97 98 /// <summary> 99 /// 获取ClassName 100 /// </summary> 101 /// <param name="url">web Server url</param> 102 /// <returns>className</returns> 103 private string GetClassName(string url) 104 { 105 string[] urlArray = url.Split(‘/‘); 106 return urlArray[urlArray.Count() - 1].Split(‘.‘)[0]; 107 } 108 }
Use Code:
1 WebServiceInvokewi = new WebServiceInvoke(); 2 byte[] byteArray = System.Text.Encoding.Default.GetBytes(XML); 3 object[] parameters = { byteArray, "0" }; 4 wi.InvokeWebService(url_to, "DisService", "ImpLes", parameters);
WebService WSDL动态访问,布布扣,bubuko.com
原文:http://www.cnblogs.com/pyffcwj/p/3772689.html