不添加引用,動態調用WebService

來源:互聯網
上載者:User

標籤:

 這兩天給公司寫一個調用介面的程式,介面採用WebService!
 本打算將這個工具寫成通用的,但是微軟給出的調用WebService的方法必須添加該WebService引用,
這樣的方式不免太過於死板,無法動態調用,於是在網上查了很多相關解決辦法!自己封裝成了一個函數,發布出來,做個紀念!

using System.Net;using System.IO;using System.Web.Services.Description;using System.CodeDom;using System.CodeDom.Compiler;using System.Reflection;using System;using System.Xml.Serialization;namespace WebServiceToOracle.Common{    /// <summary>    /// Web服務作業類    /// Author:梁    /// Time:2014-4-17    /// </summary>    public class WebServiceHelper    {        /// <summary>        /// 動態調用WebService        /// </summary>        /// <param name="url">WebService地址</param>        /// <param name="function">欲調用的WebService介面函數</param>        /// <param name="parameters">介面參數</param>        /// <returns>介面資料</returns>        public static string DynamicInvocationWebService(string url,string function,string[] parameters)         {            WebClient client = new WebClient();            if (url.IndexOf("?WSDL") == -1)             {                url = url + "?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;            //添加WSDL文檔。            importer.AddServiceDescription(description, null, null);            //命名空間            CodeNamespace nmspace = new CodeNamespace();            nmspace.Name = "SearchAmountOfProduct";            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 = "DynamicInvocationWebService.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)            {                // 顯示編譯錯誤資訊            }            //載入前面產生的程式集            Assembly asm = Assembly.LoadFrom("DynamicInvocationWebService.dll");            Type t = asm.GetType("SearchAmountOfProduct.SearchAmountOfProduct");            object o = Activator.CreateInstance(t);            MethodInfo method = t.GetMethod(function);            String item = (String)method.Invoke(o, parameters);            return item;        }    }} 

  

不添加引用,動態調用WebService

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.