动态调用webservice

来源:互联网 发布:php 开源 api文档工具 编辑:程序博客网 时间:2024/06/06 12:40

 

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
 
usingSystem.IO;
usingSystem.Configuration;
// 命名空间包含提供用于处理配置数据的编程模型的类型。
usingSystem.CodeDom;
/*代码文档对象模型”, 使用该模型建立的代码文档可以被.NET Framework编译成应用程序集。也就是说,
 *你可以使用该模型“发明”一个自己的.net语言,用你的语言编写程序,再翻译成codeDom,
 * 最后编译成可以执行的.net应用程序。
 *归根结底在System.CodeDom这个命名空间里你new来new去只能是得到一些Object而已,
 * 而Object是什么呢,就是内存里的一点点数据
 */
usingSystem.CodeDom.Compiler;
/*System.CodeDom.Compiler这个命名空间里的东西才能把它表现出来。前一个命名空间在于构造,
 * 后一个命名空间在于表现。构造就是搭个架子,把里面的各个部分聚合聚合,连接连接,
 * 这个一点点奥秘都没有,所有也不去深究了
 */
usingSystem.Net;//利用System.Net类访问因特网
usingSystem.Web.Services;
usingSystem.Web.Services.Description;
/* 命名空间由使得您可以通过使用 Web 服务描述语言 (WSDL) 来公开描述 XML Web services 的类组成。
 * 此命名空间中的每个类都与 WSDL 规范中的某个特定元素相对应,
 * 并且类的层次结构与有效的 WSDL 文档的 XML 结构相对应。
 * 有关 WSDL 的更多信息,请参见位于 W3C 网站 (http://www.w3.org/TR/wsdl/) 的规范。
 */
usingMicrosoft.CSharp;
//命名空间包含支持使用 C# 语言编译和生成代码的类。
 
namespaceInvokeWebService
{
    publicstatic class WebServiceHelper
    {
        /// <summary>
        /// 动态调用WebService
        /// </summary>
        /// <param name="url">WebService地址</param>
        /// <param name="methodname">方法名(模块名)</param>
        /// <param name="args">参数列表</param>
        /// <returns>object</returns>
        publicstatic object InvokeWebService(string url, string methodname,object[] args)
        {
            returnInvokeWebService(url, null, methodname, args);
        }
        /// <summary>
        /// 动态调用WebService
        /// </summary>
        /// <param name="url">WebService地址</param>
        /// <param name="classname">类名</param>
        /// <param name="methodname">方法名(模块名)</param>
        /// <param name="args">参数列表</param>
        /// <returns>object</returns>
        publicstatic object InvokeWebService(string url, string classname,string methodname, object[] args)
        {
            string@namespace = "ServiceBase.WebService.DynamicWebLoad";
            if(classname == null || classname == "")
            {
                classname = WebServiceHelper.GetClassName(url);
            }
            //(一):获取服务描述语言(WSDL)
 
            //提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法
            WebClient wc =new WebClient();
            Stream stream = wc.OpenRead(url +"?WSDL");
            /*下表描述从资源下载数据的 WebClient 方法.相对应的是上载到资源的 WebClient 方法。
             * OpenRea 资源以 Stream 的形式返回数据,对应与OpenWrite()
             * OpenReadAsync 在不阻止调用线程的情况下,从资源返回数据
             * DownloadData从资源下载数据并返回 Byte 数组。
             * DownloadDataAsync在不阻止调用线程的情况下,从资源下载数据并返回 Byte 数组。
             * DownloadFile从资源将数据下载到本地文件。
             * DownloadFileAsync在不阻止调用线程的情况下,将数据从资源下载到本地文件。
             * DownloadString从资源下载 String 并返回 String。
             * DownloadStringAsync在不阻止调用线程的情况下,从资源下载 String
             */
 
            //ServiceDescription 类与 WSDL 文件的根元素 definitions 相对应。
            ServiceDescription sd = ServiceDescription.Read(stream);
 
            //使用 ServiceDescriptionImporter 类可以方便地将 WSDL 说明中包含的信息导入到
            //System.CodeDom.CodeCompileUnit 对象
            ServiceDescriptionImporter sdi =new ServiceDescriptionImporter();
            sdi.AddServiceDescription(sd,"", "");
            CodeNamespace cn =new CodeNamespace(@namespace);
 
            //(二):生成客户端代理类代码
 
            /*CodeCompileUnit 包含以下几个集合:可以存储包含
             * CodeDOM 源代码图形的 CodeNamespace 对象的集合、项目引用的程序集的集合,
             * 以及项目程序集的属性集合。
             */
            CodeCompileUnit ccu =new CodeCompileUnit();
 
            // Add the new namespace to the compile unit.
            ccu.Namespaces.Add(cn);
 
            //导入指定的 ServiceDescriptions 值,并将按照 Style 属性的指定来生成代码。
            // Add the new namespace import for the System namespace
            sdi.Import(cn, ccu);
 
            //提供对 C# 代码生成器和代码编译器的实例的访问。
            CSharpCodeProvider csc =new CSharpCodeProvider();
 
            /*ICodeCompiler 接口提供用于在运行时使用指定的参数调用编译的功能,
              *以及在编译之后访问编译相关信息的功能,这些信息包括结果代码和编译器返回的任何错误或警告。
              * 每种编译方法都接受指示编译器设置的 CompilerParameters 对象,并返回指示编译结果的 CompilerResults 对象。
              */
            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)
            {
                System.Text.StringBuilder sb =new StringBuilder();
                foreach(CompilerError ce incr.Errors)
                {
                    sb.Append(ce.ToString());
                    sb.Append(System.Environment.NewLine);
                }
                thrownew Exception(sb.ToString());
            }
 
            //(五):生成代理实例,并调用方法
            System.Reflection.Assembly assembly = cr.CompiledAssembly;
            Type t = assembly.GetType(@namespace+ "." + classname, true, true);
 
            //包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用
            objectobj = Activator.CreateInstance(t);
 
            //发现方法的属性 (Attribute) 并提供对方法元数据的访问。
            System.Reflection.MethodInfo mi = t.GetMethod(methodname);
            returnmi.Invoke(obj, args);
 
        }
 
        privatestatic string GetClassName(string url)
        {
            string[] parts = url.Split('/');
            string[] pps = parts[parts.Length - 1].Split('.');
            returnpps[0];
        }
 
    }
}

 

 

转载自:http://my.oschina.net/skydog/blog/41227

0 0
原创粉丝点击