遠程WebService方法

來源:互聯網
上載者:User
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO;using System.CodeDom;using Microsoft.CSharp;using System.CodeDom.Compiler;using System.Reflection;namespace Dct.DuplexServices.Services{    internal class WebServiceHelper    {        /// <summary>        /// 根據指定的資訊,調用遠程WebService方法        /// </summary>        /// <param name="url">WebService的http形式的地址</param>        /// <param name="namespace">欲調用的WebService的命名空間</param>        /// <param name="classname">欲調用的WebService的類名(不包括命名空間首碼)</param>        /// <param name="methodname">欲調用的WebService的方法名</param>        /// <param name="args">參數列表</param>        /// <returns>WebService的執行結果</returns>        /// <remarks>        /// 如果調用失敗,將會拋出Exception。請調用的時候,適當截獲異常。        /// 異常資訊可能會發生在兩個地方:        /// 1、動態構造WebService的時候,CompileAssembly失敗。        /// 2、WebService本身執行失敗。        /// </remarks>        /// <example>        /// <code>        /// object obj = InvokeWebservice("http://localhost/test.asmx",        ///                               "dct",        ///                               "Common",        ///                               "GetToolType",        ///                               new object[]{"1"});        /// </code>        /// </example>        public static object InvokeWebservice(string url, string @namespace, string classname,                                              string methodname, object[] args)        {            try            {                WebClient wc = new WebClient();                Stream stream = wc.OpenRead(url + "?WSDL");                System.Web.Services.Description.ServiceDescription sd                    = System.Web.Services.Description.ServiceDescription.Read(stream);                System.Web.Services.Description.ServiceDescriptionImporter sdi                    = new System.Web.Services.Description.ServiceDescriptionImporter();                sdi.AddServiceDescription(sd, "", "");                CodeNamespace cn = new CodeNamespace(@namespace);                CodeCompileUnit ccu = new CodeCompileUnit();                ccu.Namespaces.Add(cn);                sdi.Import(cn, ccu);                CSharpCodeProvider csc = new CSharpCodeProvider();                ICodeCompiler icc = csc.CreateCompiler();                CompilerParameters cplist = new CompilerParameters();                cplist.GenerateExecutable = false;                cplist.GenerateInMemory = true;                cplist.ReferencedAssemblies.Add("System.dll");                cplist.ReferencedAssemblies.Add("System.XML.dll");                cplist.ReferencedAssemblies.Add("System.Web.Services.dll");                cplist.ReferencedAssemblies.Add("System.Data.dll");                CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);                if (true == cr.Errors.HasErrors)                {                    StringBuilder sb = new StringBuilder();                    foreach (CompilerError ce in cr.Errors)                    {                        sb.Append(ce.ToString());                        sb.Append(System.Environment.NewLine);                    }                    throw new Exception(sb.ToString());                }                Assembly assembly = cr.CompiledAssembly;                Type t = assembly.GetType(@namespace + "." + classname, true, true);                object obj = Activator.CreateInstance(t);                MethodInfo mi = t.GetMethod(methodname);                return mi.Invoke(obj, args);            }            catch (Exception ex)            {                throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));            }        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.