• C# 动态调用WebService 2


    using Microsoft.CSharp;
    using System;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.ServiceModel;
    using System.ServiceModel.Security;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Services.Description;
    
    public class WebServiceHelper
    {
        #region InvokeWebService   
        //动态调用web服务   
        public object InvokeWebService(string url, string methodname, object[] args)
        {
            return InvokeWebService(url, null, methodname, args);
        }
    
        public object InvokeWebService(string url, string classname,
            string methodname, object[] args)
        {
            //string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
            string @namespace = "FrameWork.WebService.DynamicWebServiceCalling";
            //FrameWork.WebService.DynamicWebServiceCalling
    
            if (string.IsNullOrEmpty(classname))
            {
                classname = GetWsClassName(url);
            }
    
            //获取WSDL   
            WebClient wc = new WebClient();
    
            url = url.Trim().ToLower().Trim("?wsdl".ToCharArray()) + "?WSDL";
            Stream stream = wc.OpenRead(url);
            ServiceDescription sd = ServiceDescription.Read(stream);
    
            sd.Bindings.Add(CreateBinding("MaxBufferSize", "2147483647"));
            sd.Bindings.Add(CreateBinding("maxReceivedMessageSize", "2147483647"));
    
            ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
            sdi.AddServiceDescription(sd, "", "");
            CodeNamespace cn = new CodeNamespace(@namespace);
    
            //生成客户端代理类代码   
            CodeCompileUnit ccu = new CodeCompileUnit();
            ccu.Namespaces.Add(cn);
            sdi.Import(cn, ccu);
    
            CodeDomProvider csc = CodeDomProvider.CreateProvider("CSharp");
    
            //设定编译参数   
            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());
            }
    
            //生成代理实例,并调用方法   
            System.Reflection.Assembly assembly = cr.CompiledAssembly;
            Type t = assembly.GetType(@namespace + "." + classname, true, true);
            object obj = Activator.CreateInstance(t);
            System.Reflection.MethodInfo mi = t.GetMethod(methodname);
            return mi.Invoke(obj, args);
        }
    
        private Binding CreateBinding(string name, string doc)
        {
            Binding bd = new Binding();
            bd.Name = name;
            bd.Documentation = doc;
            return bd;
        }
    
        private string GetWsClassName(string wsUrl)
        {
            string[] parts = wsUrl.Split('/');
            string[] pps = parts[parts.Length - 1].Split('.');
    
            return pps[0];
        }
        #endregion
    }
  • 相关阅读:
    jekins构建自动化项目的步骤
    CRT 和mysql 中文乱码解决方式
    Jenkins的配置(rpm red hat方式)
    MapReduce job.setNumReduceTasks(0)思考
    浏览器angent分析工具
    npm中的 --save-dev
    computed与methods的异同
    JS函数种类详解
    Vue.js和Nodejs的关系
    AJAX复习笔记
  • 原文地址:https://www.cnblogs.com/ilookbo/p/5549262.html
Copyright © 2020-2023  润新知