动态调用WebService方法-创新互联
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.CodeDom.Compiler;
- using System.Reflection;
- using System.Web.Services.Description;
- using System.Xml.Serialization;
- using System.CodeDom;
- namespace CN100.Member.Utility
- {
- public class WebServiceHelper
- {
- private static WebServiceHelper webService = null;
- public static WebServiceHelper Instance(string webServiceUrl, string NamSpace)
- {
- if (webService == null)
- {
- webService = new WebServiceHelper(webServiceUrl, NamSpace);
- }
- return webService;
- }
- private WebServiceHelper()
- {
- }
- ///
- /// webService地址
- ///
- public string ServerUrl
- {
- get;
- set;
- }
- ///
- /// 调用类命名空间
- ///
- public string NameSpace
- {
- get;
- set;
- }
- private WebServiceHelper(string webServiceUrl, string namSpace)
- {
- ServerUrl = webServiceUrl;
- NameSpace = namSpace;
- }
- ///
- /// 生成动态引用DLL
- ///
- ///
- public bool GenerateWebService()
- {
- WebClient client = new WebClient();
- String url = ServerUrl + "?WSDL";//这个地址可以写在Config文件里面,这里取出来就行了.在原地址后面加上: ?WSDL
- Stream stream = client.OpenRead(url);
- ServiceDescription description = ServiceDescription.Read(stream);
- ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//创建客户端代理代理类。
- importer.ProtocolName = "Soap"; //指定访问协议。
- importer.Style = ServiceDescriptionImportStyle.Client; //生成客户端代理。
- importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
- importer.AddServiceDescription(description, null, null); //添加WSDL文档。
- CodeNamespace nmspace = new CodeNamespace(); //命名空间
- nmspace.Name = NameSpace;
- CodeCompileUnit unit = new CodeCompileUnit();
- unit.Namespaces.Add(nmspace);
- ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
- CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
- CompilerParameters parameter = new CompilerParameters();
- parameter.GenerateExecutable = false;
- parameter.OutputAssembly = NameSpace + ".dll";//输出程序集的名称
- parameter.ReferencedAssemblies.Add("System.dll");
- parameter.ReferencedAssemblies.Add("System.XML.dll");
- parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
- parameter.ReferencedAssemblies.Add("System.Data.dll");
- CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
- if (result.Errors.HasErrors)
- {
- // 显示编译错误信息
- return false;
- }
- return true;
- }
- private Assembly LoadAssembly(string nameSpace)
- {
- Assembly asm = null;
- try
- {
- asm=Assembly.LoadFrom(nameSpace + ".dll");//加载前面生成的程序集
- }
- catch (FileNotFoundException ex)
- {
- if (GenerateWebService())
- {
- asm=Assembly.LoadFrom(nameSpace + ".dll");//加载前面生成的程序集
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- return asm;
- }
- ///
- /// 执行无返回值方法
- ///
- ///
- ///
- ///
- public void ExcuteMethod(string methodName,string nameSpace,object[] args)
- {
- Assembly asm = LoadAssembly(nameSpace);
- Type t = asm.GetType(nameSpace);
- object o = Activator.CreateInstance(t);
- MethodInfo method = t.GetMethod(methodName);
- method.Invoke(o, args);
- }
- public void ExcuteMethod(string methodName, object[] args)
- {
- string nameSpace = NameSpace;
- ExcuteMethod(methodName, nameSpace, args);
- }
- ///
- /// 执行带返回值方法
- ///
- ///
- ///
- ///
- ///
- ///
- public T ExcuteMethod
(string methodName, string nameSpace, object[] args) - {
- Assembly asm = LoadAssembly(nameSpace);
- Type t = asm.GetType(nameSpace);
- object o = Activator.CreateInstance(t);
- MethodInfo method = t.GetMethod(methodName);
- T result = (T)method.Invoke(o, args);
- return result;
- }
- public T ExcuteMethod
(string methodName, object[] args) - {
- string nameSpace=NameSpace;
- return ExcuteMethod
(methodName, nameSpace, args); - }
- }
- }
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
站在用户的角度思考问题,与客户深入沟通,找到庆云网站设计与庆云网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站建设、成都网站制作、企业官网、英文网站、手机端网站、网站推广、域名申请、网络空间、企业邮箱。业务覆盖庆云地区。网站名称:动态调用WebService方法-创新互联
文章地址:http://ybzwz.com/article/gpidh.html