1. 程式人生 > >動態呼叫WebService的兩種方法(多執行緒)

動態呼叫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
版權宣告:本文為博主原創文章,轉載請附上博文連結!