如何解決Webservice第一次訪問特別慢的問題
Webservice第一次訪問特別慢問題
最近做一個項目遇到首次加載webservice的時候特別慢,于是Google一番,得到結果是
<system.net> <defaultProxy enabled="false" useDefaultCredentials="false"> <proxy/> <bypasslist/> <module/> </defaultProxy> </system.net>
原理是:由于web代理默認是開啟的,也就是HttpWebRequest.DefaultWebProxy的值不為null,而這個DefaultWebProxy是一個全局變量。故第一次調用webservice方法的時候只有等這個默認代理超時以后才能繞過,所以第一次比較慢。
然而這個方法還不是特別慢的最大原因,所以即使這么做了效果依然沒有明顯的變快,于是又是一番的Google。
最終發(fā)現一個另一個因素:
原因很簡單,就是因為在第一次連接Webservice時,應用程序動態(tài)編譯生成序列化程序集導致的慢。
問題知道了那么就說說如何解決
1、首先如何提前生成序列化程序集
這個時候你會發(fā)現你的bin目錄下回生成一個“***.XmlSerializers.dll”
2、接下來就簡單了,在程序啟動的時候就把這個文件加載進來就OK了
Assembly.LoadFrom(Application.StartupPath + "\\***.XmlSerializers.dll");
/// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { bool ok; var m = new System.Threading.Mutex(true, "***.exe", out ok); if (!ok) return; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Assembly.LoadFrom(Application.StartupPath + "\\***.XmlSerializers.dll"); Application.Run(new FormMain()); GC.KeepAlive(m); }
3、騷年啟動你的應用程序吧
cxf動態(tài)調用Webservice接口
package cxfClient; import org.apache.cxf.endpoint.Endpoint; import javax.xml.namespace.QName; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.apache.cxf.service.model.BindingInfo; import org.apache.cxf.service.model.BindingOperationInfo; public class CxfClient { public static void main(String[] args) throws Exception { String url = "http://localhost:9091/Service/SayHello?wsdl"; String method = "say"; Object[] parameters = new Object[]{"我是參數"}; System.out.println(invokeRemoteMethod(url, method, parameters)[0]); } public static Object[] invokeRemoteMethod(String url, String operation, Object[] parameters){ JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); if (!url.endsWith("wsdl")) { url += "?wsdl"; } org.apache.cxf.endpoint.Client client = dcf.createClient(url); //處理webService接口和實現類namespace不同的情況,CXF動態(tài)客戶端在處理此問題時,會報No operation was found with the name的異常 Endpoint endpoint = client.getEndpoint(); QName opName = new QName(endpoint.getService().getName().getNamespaceURI(),operation); BindingInfo bindingInfo= endpoint.getEndpointInfo().getBinding(); if(bindingInfo.getOperation(opName) == null){ for(BindingOperationInfo operationInfo : bindingInfo.getOperations()){ if(operation.equals(operationInfo.getName().getLocalPart())){ opName = operationInfo.getName(); break; } } } Object[] res = null; try { res = client.invoke(opName, parameters); } catch (Exception e) { e.printStackTrace(); } return res; } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springboot2以代碼的方式統(tǒng)一配置Jackson教程
這篇文章主要介紹了Springboot2以代碼的方式統(tǒng)一配置Jackson教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11在Eclipse中部署Spring Boot/Spring Cloud應用到阿里云
這篇文章主要介紹了在Eclipse中部署Spring Boot/Spring Cloud應用到阿里云,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12SpringMvc @RequestParam 使用推薦使用包裝類型代替包裝類型
這篇文章主要介紹了SpringMvc @RequestParam 使用推薦使用包裝類型代替包裝類型,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02Java方法遞歸的形式和常見遞歸算法(方法遞歸結合File類查找文件)
方法遞歸方法直接調用自己或者間接調用自己的形式稱為方法遞歸( recursion),遞歸做為一種算法在程序設計語言中廣泛應用,這篇文章主要介紹了Java方法遞歸的形式和常見遞歸算法-方法遞歸結合File類查找文件,需要的朋友可以參考下2023-02-02詳解SpringBoot整合RabbitMQ如何實現消息確認
這篇文章主要介紹了SpringBoot整合RabbitMQ是如何實現消息確認的,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05