1、完全动态处理,传入服务服务网址,方法名和参数即可. using System; using System.Net; using System.IO; using System.CodeDom; using Microsoft.CSharp; using System.CodeDom.Compiler; using System.Web.Services.Description; using System.Web.Services.Protocols; namespace HB.Common /* 调用方式 * string url = “http:/ ; * string args = new
2、 string2 ; * args0 = “Hangzhou“; * args1 = “China“ ; * object result = WebServiceHelper.InvokeWebService(url ,“GetWeather“ ,args) ; * Response.Write(result.ToString(); */ public class WebServiceHelper #region InvokeWebService / / 动态调用web服务 / / WSDL服务地址 / 方法名 / 参数 / public static object InvokeWebServ
3、ice(string url, string methodname, object args) return WebServiceHelper.InvokeWebService(url, null, methodname, args); / / 动态调用web服务 / / WSDL服务地址 / 类名 / 方法名 / 参数 / public static object InvokeWebService(string url, string classname, string methodname, object args) string namespace = “EnterpriseServer
4、Base.WebService.DynamicWebCalling“; if (classname = null) | (classname = “) classname = WebServiceHelper.GetWsClassName(url); try /获取WSDL WebClient wc = new WebClient(); Stream stream = wc.OpenRead(url + “?WSDL“); ServiceDescription sd = ServiceDescription.Read(stream); ServiceDescriptionImporter sd
5、i = new ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, “, “); CodeNamespace cn = new CodeNamespace(namespace); /生成客户端代理类代码 CodeCompileUnit ccu = new CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); CSharpCodeProvider icc = new CSharpCodeProvider(); /设定编译参数 CompilerParamet
6、ers cplist = new CompilerParameters(); cplist.GenerateExecutable = false; cplist.GenerateInMemory = true; cplist.ReferencedAssemblies.Add(“System.dll“); cplist.ReferencedAssemblies.Add(“System.XML.dll“); cplist.ReferencedAssemblies.Add(“System.Web.Services.dll“); cplist.ReferencedAssemblies.Add(“Sys
7、tem.Data.dll“); /编译代理类 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); if (true = cr.Errors.HasErrors) System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (System.CodeDom.Compiler.CompilerError cein cr.Errors) sb.Append(ce.ToString(); sb.Append(System.Environment.Ne
8、wLine); throw new Exception(sb.ToString(); /生成代理实例,并调用方法 System.Reflection.Assembly assembly = cr.CompiledAssembly; Type t = assembly.GetType(namespace + “.“ + classname, true, true); object obj = Activator.CreateInstance(t); System.Reflection.MethodInfo mi = t.GetMethod(methodname); return mi.Invok
9、e(obj, args); /* PropertyInfo propertyInfo = type.GetProperty(propertyname); return propertyInfo.GetValue(obj, null); */ catch (Exception ex) throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace); private static string GetWsClassName(string wsUrl) string parts =
10、wsUrl.Split(/); string pps = partsparts.Length - 1.Split(.); return pps0; #endregion 返回时如果不是字符串,即强制转换,如返回是DataSet,则 string url = “http:/ ; string args = new string2 ; args0 = “Hangzhou“; args1 = “China“ ; object result = WebServiceHelper.InvokeWebService(url ,“GetWeather“ ,args) ; DataSet DSRe=(DataSet)result;