zoukankan      html  css  js  c++  java
  • C# 动态调用WebService 3

    using Microsoft.CSharp;
    using System;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using System.IO;
    using System.Net;
    using System.Reflection;
    using System.Text;
    using System.Web.Services.Description;
    
    namespace BOBO
    {
        public class MyWSDL
        {
            public object InvokeWebService3(string url, string methodname, int WSDLCacheTime, params object[] args)
            {
                return InvokeWebService(url, methodname, WSDLCacheTime, args);
            }
    
            public object InvokeWebService2(string url, string methodname, params object[] args)
            {
                return InvokeWebService(url, methodname, 60, args);
            }
    
            /// <summary>
            /// 实例化WebServices
            /// </summary>
            /// <param name="url">WebServices地址</param>
            /// <param name="methodname">调用的方法</param>
            /// <param name="args">把webservices里需要的参数按顺序放到这个object[]里</param>
            public object InvokeWebService(string url, string methodname, int WSDLCacheTime, object[] args)
            {
                try
                {
                    object obj = null;
                    MethodInfo mi = GetMethodInfo(url, methodname, ref obj, WSDLCacheTime);
                    return mi.Invoke(obj, args);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                    return null;
                }
            }
    
            MyString mstr = new MyString();
            CacheManage cm = new CacheManage();
    
            public MethodInfo GetMethodInfo(string url, string methodname, ref object obj, int WSDLCacheTime = 60 * 60)
            {
                string fname = MethodBase.GetCurrentMethod().Name;
                string key = mstr.MD5Encrypt(fname + url + methodname);
                MethodInfo mi = (MethodInfo)cm.GetFromCache(key);
                obj = cm.GetFromCache(key + "obj");
                if (mi == null)
                {
                    string @namespace = "client";
                    //获取WSDL
                    WebClient wc = new WebClient();
                    Stream stream = wc.OpenRead(url + "?WSDL");
                    ServiceDescription sd = ServiceDescription.Read(stream);
                    sd.Bindings.Add(CreateBinding("MaxBufferSize", "2147483647"));
                    sd.Bindings.Add(CreateBinding("maxReceivedMessageSize", "2147483647"));
                    string classname = sd.Services[0].Name;
                    ServiceDescriptionImporter sdi = new 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 = csc.CompileAssemblyFromDom(cplist, ccu);
                    if (true == cr.Errors.HasErrors)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (CompilerError ce in cr.Errors)
                        {
                            sb.Append(ce.ToString());
                            sb.Append(Environment.NewLine);
                        }
    
                        throw new Exception(sb.ToString());
                    }
    
                    //生成代理实例,并调用方法
                    Assembly assembly = cr.CompiledAssembly;
                    Type t = assembly.GetType(@namespace + "." + classname, true, true);
    
                    obj = Activator.CreateInstance(t);
                    mi = t.GetMethod(methodname);
                    if (WSDLCacheTime < 60)
                    {
                        WSDLCacheTime = 60;
                    }
                    cm.SetCache(key, mi, WSDLCacheTime);
                    cm.SetCache(key + "obj", obj, WSDLCacheTime + 5);
                }
                return mi;
            }
    
            private Binding CreateBinding(string name, string doc)
            {
                Binding bd = new Binding();
                bd.Name = name;
                bd.Documentation = doc;
                return bd;
            }
        }
    }
      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
            </binding>
          </basicHttpBinding>
        </bindings>
      </system.serviceModel>
  • 相关阅读:
    centos7 tar.gz zip 解压命令[转]
    centos7 下 安装visual studio code ,打开运行。
    yum提示Another app is currently holding the yum lock; waiting for it to exit...
    Linux CentOS7 安装
    虚拟机 centos7 的网络与电脑所在一个网络如何设置
    django创建web项目
    uwsgi Flask web.py
    MariaDB 与 MySQL 区别
    python pymysql 与 mysqlclient
    如何提高自己的象棋水平及象棋开局的五种忌讳
  • 原文地址:https://www.cnblogs.com/ilookbo/p/10436195.html
Copyright © 2011-2022 走看看