1 /* 调用方式 2 * string url = "http://www.webservicex.net/globalweather.asmx" ; 3 * string[] args = new string[2] ; 4 * args[0] = "Hangzhou"; 5 * args[1] = "China" ; 6 * object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ; 7 * Response.Write(result.ToString()); 8 */ 9 public class WebServiceHelper 10 { 11 #region InvokeWebService 12 /// < summary> 13 /// 动态调用web服务 14 /// < /summary> 15 /// < param name="url">WSDL服务地址< /param> 16 /// < param name="methodname">方法名< /param> 17 /// < param name="args">参数< /param> 18 /// < returns>< /returns> 19 public static object InvokeWebService(string url, string methodname, object[] args) 20 { 21 return WebServiceHelper.InvokeWebService(url, null, methodname, args); 22 } 23 24 /// < summary> 25 /// 动态调用web服务 26 /// < /summary> 27 /// < param name="url">WSDL服务地址< /param> 28 /// < param name="classname">类名< /param> 29 /// < param name="methodname">方法名< /param> 30 /// < param name="args">参数< /param> 31 /// < returns>< /returns> 32 public static object InvokeWebService(string url, string classname, string methodname, object[] args) 33 { 34 string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling"; 35 if ((classname == null) || (classname == "")) 36 { 37 classname = WebServiceHelper.GetWsClassName(url); 38 } 39 40 try 41 { 42 43 44 //获取WSDL 45 WebClient wc = new WebClient(); 46 Stream stream = wc.OpenRead(url + "?WSDL"); 47 ServiceDescription sd = ServiceDescription.Read(stream); 48 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); 49 sdi.AddServiceDescription(sd, "", ""); 50 CodeNamespace cn = new CodeNamespace(@namespace); 51 52 //生成客户端代理类代码 53 CodeCompileUnit ccu = new CodeCompileUnit(); 54 ccu.Namespaces.Add(cn); 55 sdi.Import(cn, ccu); 56 CSharpCodeProvider icc = new CSharpCodeProvider(); 57 58 //设定编译参数 59 CompilerParameters cplist = new CompilerParameters(); 60 cplist.GenerateExecutable = false; 61 cplist.GenerateInMemory = true; 62 cplist.ReferencedAssemblies.Add("System.dll"); 63 cplist.ReferencedAssemblies.Add("System.XML.dll"); 64 cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); 65 cplist.ReferencedAssemblies.Add("System.Data.dll"); 66 67 //编译代理类 68 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); 69 if (true == cr.Errors.HasErrors) 70 { 71 System.Text.StringBuilder sb = new System.Text.StringBuilder(); 72 foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) 73 { 74 sb.Append(ce.ToString()); 75 sb.Append(System.Environment.NewLine); 76 } 77 throw new Exception(sb.ToString()); 78 } 79 80 //生成代理实例,并调用方法 81 System.Reflection.Assembly assembly = cr.CompiledAssembly; 82 Type t = assembly.GetType(@namespace + "." + classname, true, true); 83 object obj = Activator.CreateInstance(t); 84 System.Reflection.MethodInfo mi = t.GetMethod(methodname); 85 86 return mi.Invoke(obj, args); 87 88 /* 89 PropertyInfo propertyInfo = type.GetProperty(propertyname); 90 return propertyInfo.GetValue(obj, null); 91 */ 92 } 93 catch (Exception ex) 94 { 95 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace)); 96 } 97 } 98 99 private static string GetWsClassName(string wsUrl) 100 { 101 string[] parts = wsUrl.Split('/'); 102 string[] pps = parts[parts.Length - 1].Split('.'); 103 104 return pps[0]; 105 } 106 #endregion 107 }
手拿菜刀砍电线,一路火花带闪电。
高楼大厦平地起,靠谁不如靠自己