在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
封装的通用方法: 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Xml; 5 using System.Net; 6 using System.Web.Services.Description; 7 using System.CodeDom; 8 using System.CodeDom.Compiler; 9 using System.Reflection; 10 namespace WindowsServiceWebDefaultHotCity 11 { 12 /// <summary< 13 /// WebService代理类 14 /// </summary< 15 public class WebServiceAgent 16 { 17 private object agent; 18 private Type agentType; 19 private const string CODE_NAMESPACE = "Beyondbit.WebServiceAgent.Dynamic"; 20 /// <summary< 21 /// 构造函数 22 /// </summary< 23 /// <param name="url"<</param< 24 public WebServiceAgent(string url) 25 { 26 XmlTextReader reader = new XmlTextReader(url + "?wsdl"); 27 28 //创建和格式化 WSDL 文档 29 ServiceDescription sd = ServiceDescription.Read(reader); 30 31 //创建客户端代理代理类 32 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); 33 sdi.AddServiceDescription(sd, null, null); 34 35 //使用 CodeDom 编译客户端代理类 36 CodeNamespace cn = new CodeNamespace(CODE_NAMESPACE); 37 CodeCompileUnit ccu = new CodeCompileUnit(); 38 ccu.Namespaces.Add(cn); 39 sdi.Import(cn, ccu); 40 Microsoft.CSharp.CSharpCodeProvider icc = new Microsoft.CSharp.CSharpCodeProvider(); 41 CompilerParameters cp = new CompilerParameters(); 42 CompilerResults cr = icc.CompileAssemblyFromDom(cp, ccu); 43 agentType = cr.CompiledAssembly.GetTypes()[0]; 44 agent = Activator.CreateInstance(agentType); 45 } 46 47 ///<summary< 48 ///调用指定的方法 49 ///</summary< 50 ///<param name="methodName"<方法名,大小写敏感</param< 51 ///<param name="args"<参数,按照参数顺序赋值</param< 52 ///<returns<Web服务的返回值</returns< 53 public object Invoke(string methodName, params object[] args) 54 { 55 MethodInfo mi = agentType.GetMethod(methodName); 56 return this.Invoke(mi, args); 57 } 58 ///<summary< 59 ///调用指定方法 60 ///</summary< 61 ///<param name="method"<方法信息</param< 62 ///<param name="args"<参数,按照参数顺序赋值</param< 63 ///<returns<Web服务的返回值</returns< 64 public object Invoke(MethodInfo method, params object[] args) 65 { 66 return method.Invoke(agent, args); 67 } 68 public MethodInfo[] Methods 69 { 70 get 71 { 72 return agentType.GetMethods(); 73 } 74 } 75 } 76 } 调用方法: string _url = "http://localhost:26711/Index.asmx"; TCMDSS_DEMO.Common.WebServiceAgent agent = new TCMDSS_DEMO.Common.WebServiceAgent(_url); object[] args = new object[3]; args[0] = "GetProductRegister"; args[1] = "037055098037050050083101114105097108078111037050050037051097037050050051050051049050037050050037055100"; args[2] = ""; string text = agent.Invoke("CallCenter", args).ToString(); MessageBox.Show(text);
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论