• 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>
  • 相关阅读:
    P2764 最小路径覆盖问题
    P1402 酒店之王 网络流
    P2597 [ZJOI2012]灾难 拓扑排序
    FJOI2017 矩阵填数
    2019.2.27模拟
    2019.2.26模拟
    SDOI2013 方程
    [AH2017/HNOI2017]抛硬币
    Lucas定理和扩展Lucas定理
    LuoguP4861 按钮
  • 原文地址:https://www.cnblogs.com/ilookbo/p/10436195.html
Copyright © 2020-2023  润新知