资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

动态调用WebService方法

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Net; 
  6. using System.IO; 
  7. using System.CodeDom.Compiler; 
  8. using System.Reflection; 
  9. using System.Web.Services.Description; 
  10. using System.Xml.Serialization; 
  11. using System.CodeDom; 
  12.  
  13. namespace CN100.Member.Utility 
  14.     public class WebServiceHelper 
  15.     { 
  16.         private static WebServiceHelper webService = null; 
  17.  
  18.         public static WebServiceHelper Instance(string webServiceUrl, string NamSpace) 
  19.         { 
  20.             if (webService == null) 
  21.             { 
  22.                 webService = new WebServiceHelper(webServiceUrl, NamSpace); 
  23.             } 
  24.             return webService; 
  25.         } 
  26.  
  27.         private WebServiceHelper() 
  28.         {  
  29.          
  30.         } 
  31.  
  32.         ///  
  33.         /// webService地址 
  34.         ///  
  35.         public string ServerUrl 
  36.         { 
  37.             get; 
  38.             set; 
  39.         } 
  40.  
  41.         ///  
  42.         /// 调用类命名空间 
  43.         ///  
  44.         public string NameSpace 
  45.         { 
  46.             get; 
  47.             set; 
  48.         } 
  49.  
  50.         private WebServiceHelper(string webServiceUrl, string namSpace) 
  51.         { 
  52.             ServerUrl = webServiceUrl; 
  53.             NameSpace = namSpace; 
  54.         } 
  55.  
  56.         ///  
  57.         /// 生成动态引用DLL 
  58.         ///  
  59.         ///  
  60.         public bool GenerateWebService() 
  61.         { 
  62.             WebClient client = new WebClient(); 
  63.             String url = ServerUrl + "?WSDL";//这个地址可以写在Config文件里面,这里取出来就行了.在原地址后面加上: ?WSDL 
  64.             Stream stream = client.OpenRead(url); 
  65.             ServiceDescription description = ServiceDescription.Read(stream); 
  66.             ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//创建客户端代理代理类。 
  67.             importer.ProtocolName = "Soap"; //指定访问协议。 
  68.             importer.Style = ServiceDescriptionImportStyle.Client; //生成客户端代理。 
  69.             importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync; 
  70.             importer.AddServiceDescription(description, null, null); //添加WSDL文档。 
  71.             CodeNamespace nmspace = new CodeNamespace(); //命名空间 
  72.             nmspace.Name = NameSpace; 
  73.             CodeCompileUnit unit = new CodeCompileUnit(); 
  74.             unit.Namespaces.Add(nmspace); 
  75.  
  76.             ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit); 
  77.             CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); 
  78.  
  79.             CompilerParameters parameter = new CompilerParameters(); 
  80.             parameter.GenerateExecutable = false; 
  81.             parameter.OutputAssembly = NameSpace + ".dll";//输出程序集的名称 
  82.             parameter.ReferencedAssemblies.Add("System.dll"); 
  83.             parameter.ReferencedAssemblies.Add("System.XML.dll"); 
  84.             parameter.ReferencedAssemblies.Add("System.Web.Services.dll"); 
  85.             parameter.ReferencedAssemblies.Add("System.Data.dll"); 
  86.  
  87.             CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit); 
  88.             if (result.Errors.HasErrors) 
  89.             { 
  90.                 // 显示编译错误信息 
  91.                 return false; 
  92.             } 
  93.             return true; 
  94.         } 
  95.  
  96.         private Assembly LoadAssembly(string nameSpace) 
  97.         { 
  98.             Assembly asm = null; 
  99.             try 
  100.             { 
  101.                 asm=Assembly.LoadFrom(nameSpace + ".dll");//加载前面生成的程序集 
  102.             } 
  103.             catch (FileNotFoundException ex) 
  104.             { 
  105.                 if (GenerateWebService()) 
  106.                 { 
  107.                     asm=Assembly.LoadFrom(nameSpace + ".dll");//加载前面生成的程序集 
  108.                 } 
  109.             } 
  110.             catch (Exception e) 
  111.             { 
  112.                 throw e; 
  113.             } 
  114.             return asm; 
  115.         } 
  116.  
  117.         ///  
  118.         /// 执行无返回值方法 
  119.         ///  
  120.         ///  
  121.         ///  
  122.         ///  
  123.         public void ExcuteMethod(string methodName,string nameSpace,object[] args) 
  124.         { 
  125.             Assembly asm = LoadAssembly(nameSpace); 
  126.             Type t = asm.GetType(nameSpace); 
  127.             object o = Activator.CreateInstance(t); 
  128.             MethodInfo method = t.GetMethod(methodName); 
  129.             method.Invoke(o, args); 
  130.         } 
  131.  
  132.         public void ExcuteMethod(string methodName, object[] args) 
  133.         { 
  134.             string nameSpace = NameSpace; 
  135.             ExcuteMethod(methodName, nameSpace, args); 
  136.         } 
  137.  
  138.         ///  
  139.         /// 执行带返回值方法 
  140.         ///  
  141.         ///  
  142.         ///  
  143.         ///  
  144.         ///  
  145.         ///  
  146.         public T ExcuteMethod(string methodName, string nameSpace, object[] args) 
  147.         { 
  148.             Assembly asm = LoadAssembly(nameSpace); 
  149.             Type t = asm.GetType(nameSpace); 
  150.             object o = Activator.CreateInstance(t); 
  151.             MethodInfo method = t.GetMethod(methodName); 
  152.             T result = (T)method.Invoke(o, args); 
  153.             return result; 
  154.         } 
  155.  
  156.         public T ExcuteMethod(string methodName, object[] args) 
  157.         { 
  158.             string nameSpace=NameSpace; 
  159.             return ExcuteMethod(methodName, nameSpace, args); 
  160.         } 
  161.  
  162.     } 

 

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了孝昌免费建站欢迎大家使用!


网站标题:动态调用WebService方法
网站路径:http://cdkjz.cn/article/jsdpcg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220