您好,登錄后才能下訂單哦!
- 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()
- {
- }
- /// <summary>
- /// webService地址
- /// </summary>
- public string ServerUrl
- {
- get;
- set;
- }
- /// <summary>
- /// 調(diào)用類命名空間
- /// </summary>
- public string NameSpace
- {
- get;
- set;
- }
- private WebServiceHelper(string webServiceUrl, string namSpace)
- {
- ServerUrl = webServiceUrl;
- NameSpace = namSpace;
- }
- /// <summary>
- /// 生成動(dòng)態(tài)引用DLL
- /// </summary>
- /// <returns></returns>
- public bool GenerateWebService()
- {
- WebClient client = new WebClient();
- String url = ServerUrl + "?WSDL";//這個(gè)地址可以寫在Config文件里面,這里取出來就行了.在原地址后面加上: ?WSDL
- Stream stream = client.OpenRead(url);
- ServiceDescription description = ServiceDescription.Read(stream);
- ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//創(chuàng)建客戶端代理代理類。
- importer.ProtocolName = "Soap"; //指定訪問協(xié)議。
- 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)
- {
- // 顯示編譯錯(cuò)誤信息
- 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;
- }
- /// <summary>
- /// 執(zhí)行無返回值方法
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="nameSpace"></param>
- /// <param name="args"></param>
- 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);
- }
- /// <summary>
- /// 執(zhí)行帶返回值方法
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="methodName"></param>
- /// <param name="nameSpace"></param>
- /// <param name="args"></param>
- /// <returns></returns>
- public T ExcuteMethod<T>(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<T>(string methodName, object[] args)
- {
- string nameSpace=NameSpace;
- return ExcuteMethod<T>(methodName, nameSpace, args);
- }
- }
- }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。