远程WebService方法

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));

            }

        }

    }

}

 

你可能感兴趣的:(webservice)