WebServiceHelper
WebServiceHelper
WebServiceHelper2
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Web; 6 7 using System.Net; 8 9 using System.Web.Services.Description; 10 11 using System.Text; 12 13 using System.IO; 14 15 using System.CodeDom; 16 17 using Microsoft.CSharp; 18 19 using System.CodeDom.Compiler; 20 21 /// <summary> 22 23 ///WebServiceHelper 的摘要说明 24 25 /// </summary> 26 27 public class WebServiceHelper 28 29 { 30 31 public WebServiceHelper() 32 33 { 34 35 // 36 37 //TODO: 在此处添加构造函数逻辑 38 39 // 40 41 } 42 43 #region InvokeWebService 44 45 //动态调用web服务 46 47 public static object InvokeWebService(string url, string methodname, object[] args) 48 49 { 50 51 return WebServiceHelper.InvokeWebService(url, null, methodname, args); 52 53 } 54 55 56 57 public static object InvokeWebService(string url, string classname, string methodname, object[] args) 58 59 { 60 61 string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling"; 62 63 if ((classname == null) || (classname == "")) 64 65 { 66 67 classname = WebServiceHelper.GetWsClassName(url); 68 69 } 70 71 72 73 try 74 75 { 76 77 //获取WSDL 78 79 WebClient wc = new WebClient(); 80 81 Stream stream = wc.OpenRead(url + "?WSDL"); 82 83 ServiceDescription sd = ServiceDescription.Read(stream); 84 85 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); 86 87 sdi.AddServiceDescription(sd, "", ""); 88 89 CodeNamespace cn = new CodeNamespace(@namespace); 90 91 92 93 //生成客户端代理类代码 94 95 CodeCompileUnit ccu = new CodeCompileUnit(); 96 97 ccu.Namespaces.Add(cn); 98 99 sdi.Import(cn, ccu); 100 101 CSharpCodeProvider csc = new CSharpCodeProvider(); 102 103 ICodeCompiler icc = csc.CreateCompiler(); 104 105 106 107 //设定编译参数 108 109 CompilerParameters cplist = new CompilerParameters(); 110 111 cplist.GenerateExecutable = false; 112 113 cplist.GenerateInMemory = true; 114 115 cplist.ReferencedAssemblies.Add("System.dll"); 116 117 cplist.ReferencedAssemblies.Add("System.XML.dll"); 118 119 cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); 120 121 cplist.ReferencedAssemblies.Add("System.Data.dll"); 122 123 124 125 //编译代理类 126 127 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); 128 129 if (true == cr.Errors.HasErrors) 130 131 { 132 133 System.Text.StringBuilder sb = new System.Text.StringBuilder(); 134 135 foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) 136 137 { 138 139 sb.Append(ce.ToString()); 140 141 sb.Append(System.Environment.NewLine); 142 143 } 144 145 throw new Exception(sb.ToString()); 146 147 } 148 149 150 151 //生成代理实例,并调用方法 152 153 System.Reflection.Assembly assembly = cr.CompiledAssembly; 154 155 Type t = assembly.GetType(@namespace + "." + classname, true, true); 156 157 object obj = Activator.CreateInstance(t); 158 159 System.Reflection.MethodInfo mi = t.GetMethod(methodname); 160 161 162 163 return mi.Invoke(obj, args); 164 165 } 166 167 catch (Exception ex) 168 169 { 170 171 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace)); 172 173 } 174 175 } 176 177 178 179 private static string GetWsClassName(string wsUrl) 180 181 { 182 183 string[] parts = wsUrl.Split('/'); 184 185 string[] pps = parts[parts.Length - 1].Split('.'); 186 187 188 189 return pps[0]; 190 191 } 192 193 #endregion 194 195 }
调用
string url = "http://www.webservicex.net/globalweather.asmx" ; string[] args = new string[2] ; args[0] = "Hangzhou"; args[1] = "China" ; object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ; DataSet DSRe=(DataSet)result;