动态调用WebService的两种方法(多线程)

转载:https://blog.csdn.net/huanglan513/article/details/46930393

在.net中,可以添加Web 引用来添加WebService,但是这种方法的缺陷是当WebService内的方法一变动,引用的系统这边就必须更新引用,重新编译,再发布,是不是很麻烦?也未可预知?

               那么就使用动态调用WebService吧!

第1种,具体步骤:

1. 从目标 URL 下载 WSDL 数据。
2. 使用 ServiceDescription 创建和格式化 WSDL 文档文件。
3. 使用 ServiceDescriptionImporter 创建客户端代理类。
4. 使用 CodeDom 动态创建客户端代理类程序集。
5. 利用反射调用相关 WebService 方法。


代码如下:

    /// <summary>
        /// 在.Net环境 下,最常用的方法就是采用代理类来调用WebService,可以通过改变代理类的Url属性来实现动态调用,
        /// 但当xmlns改变时就会出错,似乎要重新 绑定Webservice并重新编译后才能再次运行。
        /// 此法子是一种动态编译并动态调用WebService的方式,这种方法效率低,而且需要有较高 的权限,否则编译失败。
        /// 此法子的缺陷。。。。。都是泪啊。。。多线程下运行第二次就报错:无法从命名空间“****”导入绑定“SyncToContractDataCenterSoap”
        /// </summary>
        public class WebServiceHelper2
        {
            #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 = "TransactionDataSync.Common";
                if ((classname == null) || (classname == ""))
                {
                    classname = GetWsClassName(url);
                }
     
                try
                {
                    //获取WSDL
                    WebClient wc = new WebClient();
                    Stream stream = wc.OpenRead(url + "?wsdl");
                    ServiceDescription sd = ServiceDescription.Read(stream);
                    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();
                    CodeDomProvider provider = 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 = provider.CompileAssemblyFromDom(cplist, ccu);
                //    CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
                    if (true == cr.Errors.HasErrors)
                    {
                        System.Text.StringBuilder sb = new System.Text.StringBuilder();
                        foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                        {
                            sb.Append(ce.ToString());
                            sb.Append(System.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);
                }
                catch (Exception ex)
                {
                    Logger.Error(string.Format("{0} {1} {2} {3}", url, methodname, ex.Message, ex.StackTrace));
                    throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
                }
            }
     
            public  object CreateWebServiceInstance(string url, string packageName, out Type t)
            {
                return CreateWebServiceInstance(url, null,packageName, out t);
            }
     
            public  object CreateWebServiceInstance(string url, string classname,string packageName,out Type t)
            {
                string @namespace = "TransactionDataSync.Common."+packageName;
                if ((classname == null) || (classname == ""))
                {
                    classname = GetWsClassName(url);
                }
                Stream stream = null;
                try
                {
                    //获取WSDL
                    WebClient wc = new WebClient();
                    stream = wc.OpenRead(url + "?WSDL");
     
                    ServiceDescription sd = ServiceDescription.Read(stream);
                    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();
                    CodeDomProvider provider = 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 = provider.CompileAssemblyFromDom(cplist, ccu);
                    //    CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
                    if (true == cr.Errors.HasErrors)
                    {
                        System.Text.StringBuilder sb = new System.Text.StringBuilder();
                        foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                        {
                            sb.Append(ce.ToString());
                            sb.Append(System.Environment.NewLine);
                        }
                        throw new Exception(sb.ToString());
                    }
     
                    
     
                    //生成代理实例
                    System.Reflection.Assembly assembly = cr.CompiledAssembly;
                    t = assembly.GetType(@namespace + "." + classname, true, true);
                  
                    object obj = Activator.CreateInstance(t);
                    return obj;
                }
                catch (Exception ex)
                {
                    Logger.Error(string.Format("{0} {1} {2}", url, ex.Message, ex.StackTrace));
     
                    if(stream!=null)
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                    throw ex;
                }
            }
     
            public  object InvokeMethod(object obj, Type t, string methodname, object[] args)
            {
                try
                {
                    System.Reflection.MethodInfo mi = t.GetMethod(methodname);
     
                    return mi.Invoke(obj, args);
                }
                catch(Exception ex)
                {
                    Logger.Error(string.Format("{0} {1} {2}", methodname, ex.Message, ex.StackTrace));
                    throw ex;
                }
            }
     
            private  string GetWsClassName(string wsUrl)
            {
                string[] parts = wsUrl.Split('/');
                string[] pps = parts[parts.Length - 1].Split('.');
     
                return pps[0];
            }
            #endregion

调用时的代码:

      object instance = WebServiceHelper2.CreateWebServiceInstance(addr.Url,name, out t);
     
     DataSet dsContract = WebServiceHelper2.InvokeMethod(instance, t, "GetContractData", new object[]{authorizationCode}) as DataSet;

 

缺陷:

        每次调用WebService,都需要动态创建客户端代理类程序集,然后利用反射去调用方法,每次都这样啊,是不是很耗性能?特别是后台服务中调WebService,1小时运行一次,是不是很无语。。。。
        正常情况下,调一个WebService是不成问题的,可是在多线程下,每个线程调一个WebService(都是不同URL的WebService),只有在编译后调第一遍是正常的,再调就报错啊,无法从命名空间“****”导入绑定“SyncToContractDataCenterSoap”,或者是调用目标异常,坑爹啊  

               第2种动态调用的法子:利用WebRequest/WebResponse进行WebService调用的类

                动态调用的类如下:
       

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
     
    namespace TransactionDataSync.Common
    {
        /// <summary>
        /// 利用WebRequest/WebResponse进行WebService调用的类
        /// </summary>
        /// <remarks>
        /// 作成者:cyf
        /// </remarks>
        public class WebServiceCaller
        {
             //缓存xmlNamespace,避免重复调用GetNamespace
            private static Hashtable XML_NAMESPACE = new Hashtable();
     
            /// <summary>
            /// 通过SOAP协议动态调用webservice
            /// </summary>
            /// <param name="url"> webservice地址</param>
            /// <param name="methodName"> 调用方法名</param>
            /// <param name="pars"> 参数表</param>
            /// <returns> 结果集xml</returns>
            public static XmlDocument QuerySoapWebService(String url, String methodName, Hashtable pars)
            {
                if (XML_NAMESPACE.ContainsKey(url))
                {   // 名字空间在缓存中存在时,读取缓存,然后执行调用
                    return QuerySoapWebService(url, methodName, pars, XML_NAMESPACE[url].ToString());
                }
                else
                {
                    // 名字空间不存在时直接从wsdl的请求中读取名字空间,然后执行调用
                    return QuerySoapWebService(url, methodName, pars, GetNamespace(url));
                }
            }
     
            /// <summary>
            /// 通过SOAP协议动态调用webservice  
            /// </summary>
            /// <param name="url"> webservice地址</param>
            /// <param name="methodName"> 调用方法名</param>
            /// <param name="pars"> 参数表</param>
            /// <param name="xmlNs"> 名字空间</param>
            /// <returns> 结果集</returns>
            private static XmlDocument QuerySoapWebService(String url, String methodName, Hashtable pars, string xmlNs)
            {   
                XML_NAMESPACE[url] = xmlNs;//加入缓存,提高效率
                // 获取请求对象
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                // 设置请求head
                request.Method = "POST";
                request.ContentType = "text/xml; charset=utf-8";
                request.Headers.Add("SOAPAction", "\"" + xmlNs + (xmlNs.EndsWith("/") ? "" : "/") + methodName + "\"");
                // 设置请求身份
                SetWebRequest(request);
                // 获取soap协议
                byte[] data = EncodeParsToSoap(pars, xmlNs, methodName);
                // 将soap协议写入请求
                WriteRequestData(request, data);
                XmlDocument returnDoc = new XmlDocument();
                XmlDocument returnValueDoc = new XmlDocument();
                // 读取服务端响应
                returnDoc = ReadXmlResponse(request.GetResponse());
     
                XmlNamespaceManager mgr = new XmlNamespaceManager(returnDoc.NameTable);
                mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
                // 返回结果
                string RetXml= returnDoc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;
     
                returnValueDoc.LoadXml("<root>" + RetXml + "</root>");
                AddDelaration(returnValueDoc);
     
              /*  System.Data.DataSet ds = new System.Data.DataSet();
                XmlNodeReader reader = new XmlNodeReader(returnValueDoc);
                ds.ReadXml(reader);*/
     
               // return returnValueDoc.OuterXml;
     
                return returnValueDoc;
            }
     
            /// <summary>
            /// 获取wsdl中的名字空间
            /// </summary>
            /// <param name="url"> wsdl地址</param>
            /// <returns> 名字空间</returns>
            private static string GetNamespace(String url)
            {
                // 创建wsdl请求对象,并从中读取名字空间
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?WSDL");
                SetWebRequest(request);
                WebResponse response = request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(sr.ReadToEnd());
                sr.Close();
                return doc.SelectSingleNode("//@targetNamespace").Value;
            }
     
            /// <summary>
            /// 加入soapheader节点
            /// </summary>
            /// <param name="doc"> soap文档</param>
            private static void InitSoapHeader(XmlDocument doc)
            {
                // 添加soapheader节点
                XmlElement soapHeader = doc.CreateElement("soap", "Header", "http://schemas.xmlsoap.org/soap/envelope/");
                //XmlElement soapId = doc.CreateElement("userid");
                //soapId.InnerText = ID;
                //XmlElement soapPwd = doc.CreateElement("userpwd");
                //soapPwd.InnerText = PWD;
                //soapHeader.AppendChild(soapId);
                //soapHeader.AppendChild(soapPwd);
                doc.ChildNodes[0].AppendChild(soapHeader);
            }
     
            /// <summary>
            /// 将以字节数组的形式返回soap协议
            /// </summary>
            /// <param name="pars"> 参数表</param>
            /// <param name="xmlNs"> 名字空间</param>
            /// <param name="methodName"> 方法名</param>
            /// <returns> 字节数组</returns>
            private static byte[] EncodeParsToSoap(Hashtable pars, String xmlNs, String methodName)
            {
                XmlDocument doc = new XmlDocument();
                // 构建soap文档
                doc.LoadXml("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"></soap:Envelope>");
     
                // 加入soapbody节点
                InitSoapHeader(doc);
     
                // 创建soapbody节点
                XmlElement soapBody = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
                // 根据要调用的方法创建一个方法节点
                XmlElement soapMethod = doc.CreateElement(methodName);
                soapMethod.SetAttribute("xmlns", xmlNs);
                // 遍历参数表中的参数键
                foreach (string key in pars.Keys)
                {
                    // 根据参数表中的键值对,生成一个参数节点,并加入方法节点内
                    XmlElement soapPar = doc.CreateElement(key);
                    soapPar.InnerXml = ObjectToSoapXml(pars[key]);
                    soapMethod.AppendChild(soapPar);
                }
     
                // soapbody节点中加入方法节点
                soapBody.AppendChild(soapMethod);
     
                // soap文档中加入soapbody节点
                doc.DocumentElement.AppendChild(soapBody);
     
                // 添加声明
                AddDelaration(doc);
     
                // 传入的参数有DataSet类型,必须在序列化后的XML中的diffgr:diffgram/NewDataSet节点加xmlns='' 否则无法取到每行的记录。
                XmlNode node = doc.DocumentElement.SelectSingleNode("//NewDataSet");   
                if (node != null)
                {
                    XmlAttribute attr = doc.CreateAttribute("xmlns");
                    attr.InnerText = "";
                    node.Attributes.Append(attr);
                }
                // 以字节数组的形式返回soap文档
                return Encoding.UTF8.GetBytes(doc.OuterXml);
            }
     
            /// <summary>
            /// 将参数对象中的内容取出
            /// </summary>
            /// <param name="o">参数值对象</param>
            /// <returns>字符型值对象</returns>
            private static string ObjectToSoapXml(object o)
            {
                XmlSerializer mySerializer = new XmlSerializer(o.GetType());
                MemoryStream ms = new MemoryStream();
                mySerializer.Serialize(ms, o);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
                if (doc.DocumentElement != null)
                {
                    return doc.DocumentElement.InnerXml;
                }
                else
                {
                    return o.ToString();
                }
            }
     
            /// <summary>
            /// 设置请求身份
            /// </summary>
            /// <param name="request"> 请求</param>
            private static void SetWebRequest(HttpWebRequest request)
            {
                request.Credentials = CredentialCache.DefaultCredentials;
                //request.Timeout = 10000;
            }
     
            /// <summary>
            /// 将soap协议写入请求
            /// </summary>
            /// <param name="request"> 请求</param>
            /// <param name="data"> soap协议</param>
            private static void WriteRequestData(HttpWebRequest request, byte[] data)
            {
                request.ContentLength = data.Length;
                Stream writer = request.GetRequestStream();
                writer.Write(data, 0, data.Length);
                writer.Close();
            }
     
            /// <summary>
            /// 将响应对象读取为xml对象
            /// </summary>
            /// <param name="response"> 响应对象</param>
            /// <returns> xml对象</returns>
            private static XmlDocument ReadXmlResponse(WebResponse response)
            {
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                String retXml = sr.ReadToEnd();
                sr.Close();
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retXml);
                return doc;
            }
     
            /// <summary>
            /// 给xml文档添加声明
            /// </summary>
            /// <param name="doc"> xml文档</param>
            private static void AddDelaration(XmlDocument doc)
            {
                XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.InsertBefore(decl, doc.DocumentElement);
            }
     
     
            public static String QuerySoapWebServiceString(String url, String methodName, Hashtable pars)
            {
                XmlDocument doc = QuerySoapWebService(url, methodName, pars);
                return doc.InnerText;
            }
     
            public static DataSet QuerySoapWebServiceDataSet(String url, String methodName, Hashtable pars)
            {
                XmlDocument doc = QuerySoapWebService(url, methodName, pars);
                System.Data.DataSet ds = new System.Data.DataSet();
                using (XmlNodeReader reader = new XmlNodeReader(doc))
                {
                    ds.ReadXml(reader);
                }
                return ds;
            }
        }
    }


使用时的调用如下:

     Hashtable htParms = new Hashtable();
                    htParms.Add("authorizationCode", authCode);
                    DataSet dsComm = WebServiceCaller.QuerySoapWebServiceDataSet(url, "GetReceivedCommissionData", htParms);


或者

     DataSet dsSelected = new DataSet();
                dsSelected.Tables.Add(dtSelected);  //需要返回给业务系统的数据集,表示哪些数据已收集
     
                Hashtable htParms = new Hashtable();
                htParms.Add("authorizationCode", addr.AuthorizationCode);
                htParms.Add("dsData", dtSelected);
     string result = WebServiceCaller.QuerySoapWebServiceString(url, methodName, htParms);//传入的参数是一个String类型和一个DataSet类型


动态调用时用的是Soap协议的方式来请求的,而不是Http Post,因为当WebService中的方法传入参数中有DataSet类型,将无法用Http Post方式请求,只能用Soap。

在多线程中,同时调多个WebService,此种方法不会报错。因此我选择了它。。。。
---------------------
作者:莫疏
来源:CSDN
原文:https://blog.csdn.net/huanglan513/article/details/46930393
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值