setting a timeout for web service response

  • Thread starter Thread starter cj2
  • Start date Start date
C

cj2

I have a windows app that calls a web service. If the web service
requires more than 20 seconds to reply I would like to timeout waiting
for a response from it and proceed on with processing. Is there any way
to set a timeout?

It's a windows form application and the web service is legacy asp.net.
 
This requires that you either issue the HTTP request either
asynchronously or as a background thread (which can be more complex to
setup).

The socket API select() function helps prepare an async I/O request
with timeout values provided.

Generally, a normal sync connect is can he used because thats either
going to be A) blocked or B) not server on the port is listening.

When blocked, typically, the response is fast. When there is no
listening server, then the default timeout is around 30 seconds.

So if you want a 20 second time, then you need to do use select()
before with the the connect command and loop/wait for the response.

Same with idea with write (sending the request), by that point you are
probably ok to just use a sync write, but you can use select here as well.

But if you think the remote server could take long, then definitely
need to use an async read request (select() + read()) otherwise it
will only return when A) it finishes or B) the socket is broken
(connection drop).
 
Hi cj2,

Yes, there is a way to set the timeout. But the approach differs from the
legacy SoapHttpClientProtocol client and the WCF client.

I'm going to explain both ways.

If you're using the SoapHttpClientProtocol client (in your project, the
reference to the Web Service is under the *Web References* folder):

You can just set the timeout value via the Timeout property.

For example:

Dim svc As New localhost.Service1 ' <- this is my web service reference

svc.Timeout = 5000 ' <- timeout after 5 seconds
MsgBox(svc.HelloWorld()) ' call the HelloWorld method on the service.

Just like Mr. Arnold said, but the Timeout value unit is milliseconds, not
seconds.

If the web service took more than 5 seconds to return the result, a
WebException will be raised on your windows application side with a message
"The operation has timed out".

Setting the Timeout property to Timeout.Infinite indicates that the request
does not time out. Even though an XML Web service client can set the
Timeout property to not time out, the Web server can still cause the
request to time out on the server side.

For more information, please refer to:
http://msdn.microsoft.com/en-us/library/system.web.services.protocols.webcli
entprotocol.timeout.aspx

***************

If you're using the WCF client (in your project, the reference to the Web
Service is under the *Service References* folder):

You can config the timeout in the app.config, where you can find a <binding
... /> section, there is a sendTimeout attribute indicating the timeout
value. The default timeout value is 1 minute.

For more information, please refer to:
http://msdn.microsoft.com/en-us/library/system.servicemodel.basichttpbinding
.aspx

***************

Also, you can config the timeout on the Web Service side (server side) by
configuring <httpRuntime ... executionTimeout />.

For more information, please refer to:
http://msdn.microsoft.com/en-us/library/e1f13641.aspx

Hope these information helps.

If you need further assistance, please kindly let me know.

Best regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks. That's what I wanted to know.
Hi cj2,

Yes, there is a way to set the timeout. But the approach differs from the
legacy SoapHttpClientProtocol client and the WCF client.

I'm going to explain both ways.

If you're using the SoapHttpClientProtocol client (in your project, the
reference to the Web Service is under the *Web References* folder):

You can just set the timeout value via the Timeout property.

For example:

Dim svc As New localhost.Service1 ' <- this is my web service reference

svc.Timeout = 5000 ' <- timeout after 5 seconds
MsgBox(svc.HelloWorld()) ' call the HelloWorld method on the service.

Just like Mr. Arnold said, but the Timeout value unit is milliseconds, not
seconds.

If the web service took more than 5 seconds to return the result, a
WebException will be raised on your windows application side with a message
"The operation has timed out".

Setting the Timeout property to Timeout.Infinite indicates that the request
does not time out. Even though an XML Web service client can set the
Timeout property to not time out, the Web server can still cause the
request to time out on the server side.

For more information, please refer to:
http://msdn.microsoft.com/en-us/library/system.web.services.protocols.webcli
entprotocol.timeout.aspx

***************

If you're using the WCF client (in your project, the reference to the Web
Service is under the *Service References* folder):

You can config the timeout in the app.config, where you can find a <binding
.. /> section, there is a sendTimeout attribute indicating the timeout
value. The default timeout value is 1 minute.

For more information, please refer to:
http://msdn.microsoft.com/en-us/library/system.servicemodel.basichttpbinding
.aspx

***************

Also, you can config the timeout on the Web Service side (server side) by
configuring <httpRuntime ... executionTimeout />.

For more information, please refer to:
http://msdn.microsoft.com/en-us/library/e1f13641.aspx

Hope these information helps.

If you need further assistance, please kindly let me know.

Best regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top