C# 动态调用WebService
WebserviceHelper帮助类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ServiceModel; 6 using System.ServiceModel.Channels; 7 using System.Net; 8 using System.Web.Services.Description; 9 using System.CodeDom; 10 using Microsoft.CSharp; 11 using System.CodeDom.Compiler; 12 using System.IO; 13 14 namespace Common 15 { 16 public class WebServiceHelper 17 { 18 #region 动态调用WebService动态调用地址 19 /// < summary> 20 /// 动态调用web服务 21 /// < /summary> 22 /// < param name="url">WSDL服务地址< /param> 23 /// < param name="methodname">方法名< /param> 24 /// < param name="args">参数< /param> 25 /// < returns>< /returns> 26 public static object InvokeWebService(string url, string methodname, object[] args) 27 { 28 return WebServiceHelper.InvokeWebService(url, null, methodname, args); 29 } 30 /// <summary> 31 /// 动态调用web服务 32 /// </summary> 33 /// <param name="url">WSDL服务地址</param> 34 /// <param name="classname">服务接口类名</param> 35 /// <param name="methodname">方法名</param> 36 /// <param name="args">参数值</param> 37 /// <returns></returns> 38 public static object InvokeWebService(string url, string classname, string methodname, object[] args) 39 { 40 41 string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling"; 42 if ((classname == null) || (classname == "")) 43 { 44 classname = WebServiceHelper.GetWsClassName(url); 45 } 46 try 47 { 48 49 //获取WSDL 50 WebClient wc = new WebClient(); 51 Stream stream = wc.OpenRead(url + "?wsdl"); 52 ServiceDescription sd = ServiceDescription.Read(stream); 53 //注意classname一定要赋值获取 54 classname = sd.Services[0].Name; 55 56 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); 57 sdi.AddServiceDescription(sd, "", ""); 58 CodeNamespace cn = new CodeNamespace(@namespace); 59 60 //生成客户端代理类代码 61 CodeCompileUnit ccu = new CodeCompileUnit(); 62 ccu.Namespaces.Add(cn); 63 sdi.Import(cn, ccu); 64 CSharpCodeProvider icc = new CSharpCodeProvider(); 65 66 67 //设定编译参数 68 CompilerParameters cplist = new CompilerParameters 69 { 70 GenerateExecutable = false, 71 GenerateInMemory = true 72 }; 73 cplist.ReferencedAssemblies.Add("System.dll"); 74 cplist.ReferencedAssemblies.Add("System.XML.dll"); 75 cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); 76 cplist.ReferencedAssemblies.Add("System.Data.dll"); 77 //编译代理类 78 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); 79 if (true == cr.Errors.HasErrors) 80 { 81 System.Text.StringBuilder sb = new System.Text.StringBuilder(); 82 foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) 83 { 84 sb.Append(ce.ToString()); 85 sb.Append(System.Environment.NewLine); 86 } 87 throw new Exception(sb.ToString()); 88 } 89 //生成代理实例,并调用方法 90 System.Reflection.Assembly assembly = cr.CompiledAssembly; 91 Type t = assembly.GetType(@namespace + "." + classname, true, true); 92 object obj = Activator.CreateInstance(t); 93 System.Reflection.MethodInfo mi = t.GetMethod(methodname); 94 return mi.Invoke(obj, args); 95 96 } 97 catch (Exception ex) 98 { 99 //throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace)); 100 return "Error:WebService调用错误!" + ex.Message; 101 } 102 } 103 private static string GetWsClassName(string wsUrl) 104 { 105 string[] parts = wsUrl.Split('/'); 106 string[] pps = parts[parts.Length - 1].Split('.'); 107 return pps[0]; 108 } 109 #endregion 110 } 111 }
调用方法:
1 /// <summary> 2 /// 调用操作 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private static string Operation(string msgHeader,string msgBody) 7 { 8 #region 动态调用 webservices地址 9 string message = string.Empty; 10 try 11 { 12 object[] args = new object[2]; 13 args[0] = msgHeader; 14 args[1] = msgBody; 15 object result = WebServiceHelper.InvokeWebService("http://192.168.1.100:8000/Service.asmx", "serviceMethod", args); 16 message = result.ToString(); 17 } 18 catch { } 19 return message; 20 #endregion 21 }