Webservice第一次访问特别慢

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Webservice第一次访问特别慢

太阳99   2022-09-08 我要评论

Webservice第一次访问特别慢问题

最近做一个项目遇到首次加载webservice的时候特别慢,于是Google一番,得到结果是

<system.net>
    <defaultProxy enabled="false" useDefaultCredentials="false">
      <proxy/>
      <bypasslist/>
      <module/>
    </defaultProxy>
  </system.net>

原理是:由于web代理默认是开启的,也就是HttpWebRequest.DefaultWebProxy的值不为null,而这个DefaultWebProxy是一个全局变量。故第一次调用webservice方法的时候只有等这个默认代理超时以后才能绕过,所以第一次比较慢。

然而这个方法还不是特别慢的最大原因,所以即使这么做了效果依然没有明显的变快,于是又是一番的Google。

最终发现一个另一个因素:

原因很简单,就是因为在第一次连接Webservice时,应用程序动态编译生成序列化程序集导致的慢。

问题知道了那么就说说如何解决

1、首先如何提前生成序列化程序集

这个时候你会发现你的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动态调用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动态客户端在处理此问题时,会报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;
    } 	
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们