调用webservice超时问题的解决

公司后台查询数据时,如果选择的时间段过长,就会遇到"请求超时"的的问题。

造成这一问题的原因大概有以下几点:

1,Asp.net请求超时 
2,Webservice请求超时 
3,IIS请求超时 
4,数据库连接超时 

 知道原因后,就可以解决问题了。

Asp.net中关于超时的设置:

在web.config 里<system.web>节点添加以下代码: 
[csharp] [/csharp] 
来自MSDN解释: 
httpRuntime是配置asp.Net http运行时设置,以确定如何处理对asp.Net应用程序的请求。 
executionTimeout:表示允许执行请求的最大时间限制,单位为秒 maxRequestLength:指示 ASP.Net 支持的最大文件上载大小。该限制可用于防止因用户将大量文件传递到该服务器而导致的拒绝服务攻击。指定的大小以 KB 为单位。默认值为 4096 KB (4 MB)。

WebService请求超时时间的设置:

扩大代理类的超时限制,默认是90秒  ,即在调用方法前指定超时时间。 
[csharp]YourWebService yws = new YourWebService(); yws.Timeout = 1200000; //20分钟,单位是毫秒[/csharp] 
如果将 Timeout 属性设置为 Timeout.Infinite,则指示该请求无超时。即使 XML Web services 客户端可以将 Timeout 属性设置为无超时,Web 服务器仍可以在服务器端使请求超时。

IIS中请求超时设置。 

IIS-网站-属性 连接超时时间 1200秒

数据库连接(ado.net)超时

1,可能是连接池(Connection Pooling)中的链接用完了,需要扩大连接池。 
2,给sqlcommand设置一个更长的超时时间(timeout属性)。 
[csharp] SqlCommand myCommand = new SqlCommand(); myCommand.CommandTimeout = 15; [/csharp] 
3,ado.net超时比较复杂,还可能是表被锁定或者其他的别的原因。 

题后:其他开发平台(jsp,php)中“请求超时”问题的解决应该有类似的方法。例如设置apche,tomcat的超时时间更长些。

 

1、web.config配置,<system.web></system.web>里面增加:<httpRuntime maxRequestLength="10240" appRequestQueueLimit="100" useFullyQualifiedRedirectUrl="true" executionTimeout="1200" />

2、扩大代理类的超时限制,默认是90秒   
YourWebService yws = new YourWebService(); 
yws.Timeout =   1200000; //20分钟

3、IIS属性-网站 连接超时时间 1200秒

 

1、修改 app.config 文件,添加如下代码:

<httpRuntime executionTimeout="600" />

请求执行超时时间为600秒(默认为110秒)

2、设置 Web services 的 Timeout 属性

对 XML Web services 的同步调用的超时(以毫秒为单位)。默认为 100000 毫秒。

lywSqCommon.sqsdData.GetData getData = new lywSqCommon.sqsdData.GetData();//GetData 为类名

getData.Timeout=700000;//单位为毫秒

指示 XML Web services 客户端等待同步 XML Web services 请求完成的时间(以毫秒计)。

提示:如果将 Timeout 属性设置为 Timeout.Infinite,则指示该请求无超时。即使 XML Web services 客户端可以将 Timeout 属性设置为无超时,Web 服务器仍可以在服务器端使请求超时。

系统将以上面两项设置的最小者作为操作超时的时间长度。


文章出处:http://www.diybl.com/course/1_web/webjs/20071021/78741.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用 WebService 进行远程调用时,由于网络不稳定等原因,可能会出现超时异常。为了防止这种情况,我们可以在代码中进行异常捕获并重新调用 WebService。 以下是一个示例代码: ```java public class WebServiceUtil { private static final int MAX_RETRY_TIMES = 3; // 最大重试次数 /** * 调用 WebService * * @param url WebService 地址 * @param method WebService 方法名 * @param timeout 超时时间(单位:毫秒) * @param params 参数列表 * @param <T> 返回类型 * @return 返回结果 * @throws Exception */ public static <T> T invokeWebService(String url, String method, int timeout, Object[] params, Class<T> resultType) throws Exception { T result = null; int retryTimes = 0; while (retryTimes < MAX_RETRY_TIMES) { try { // 创建服务调用工厂 ServiceFactory serviceFactory = ServiceFactory.newInstance(); Call call = (Call) serviceFactory.createCall(); // 设置 WebService 地址 call.setTargetEndpointAddress(new URL(url)); // 设置 WebService 方法名 call.setOperationName(method); // 设置 WebService 超时时间 call.setTimeout(timeout); // 设置 WebService 参数 call.setUseSOAPAction(true); call.addParameter(new QName(method), XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(XMLType.XSD_STRING); // 调用 WebService String resultStr = (String) call.invoke(params); // 解析 WebService 返回结果 JAXBContext jaxbContext = JAXBContext.newInstance(resultType); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = new StringReader(resultStr); result = (T) unmarshaller.unmarshal(reader); break; } catch (Exception e) { retryTimes++; if (retryTimes == MAX_RETRY_TIMES) { throw e; } } } return result; } } ``` 在调用 WebService 的过程中,我们可以设置最大重试次数。当出现异常时,我们可以进行重试,直到达到最大重试次数或者成功调用 WebService。如果重试次数达到最大限制仍然失败,则抛出异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值