C#中异步Web服务调用的超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/536629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
timeout for asynchronous web service call in C#
提问by George2
I am using C# and created a proxy from a ASP.Net asmx Web Service at client side, by using Add Web Reference... feature of VSTS 2008.
我正在使用 C# 并通过使用 VSTS 2008 的 Add Web Reference... 功能从客户端的 ASP.Net asmx Web 服务创建了一个代理。
And I am using the asynchronous method call model (i.e. call the AsyncXXX method, which will return immediately) and handles the complete event (I add event handler to handle the complete even when the reponse is received from server side).
我正在使用异步方法调用模型(即调用 AsyncXXX 方法,它将立即返回)并处理完整事件(即使从服务器端收到响应,我添加事件处理程序来处理完整事件)。
I find if there is no response from server for a long time, the complete event will not be fired.
我发现如果服务器长时间没有响应,则不会触发 complete 事件。
My questions are,
我的问题是,
Is it expected feature or my bug that if no response from server after call AsyncXXX method, complete event handler will not be called?
Are there any way to assign timeout value -- so that I do not wait on the complete event handler infinitely?
如果在调用 AsyncXXX 方法后服务器没有响应,则不会调用完整的事件处理程序,这是预期的功能还是我的错误?
有没有办法分配超时值——这样我就不会无限等待完整的事件处理程序?
thanks in advance, George
提前致谢,乔治
回答by Micha?l Carpentier
SoapHttpClientProtocol
which your proxy should inherit from has a Timeout
property inherited from its ancestor WebClientProtocol
. It could answer your second question.
SoapHttpClientProtocol
您的代理应继承的Timeout
属性具有从其祖先继承的属性WebClientProtocol
。它可以回答你的第二个问题。
回答by cjk
In the web.config for your webservice you can set a timeout (You will also need to change to not be in debug mode if it is). This should then cause it to throw an exception that will raise your completed event. You will then need to inspect for an exception to see if it passed or failed.
在您的 web 服务的 web.config 中,您可以设置超时(如果是,您还需要更改为不处于调试模式)。这应该会导致它抛出一个异常,该异常将引发您的完成事件。然后,您需要检查异常以查看它是通过还是失败。
<system.web>
<httpRuntime executionTimeout="120" />
</system.web>
回答by Kelly
If you want your web service to never timeout you can put this line in the web services class:
如果您希望您的 Web 服务永不超时,您可以将此行放在 Web 服务类中:
this.Timeout = System.Threading.Timeout.Infinite;