Calling web services from .NET

S

Scott Baierl

I have an ASP.NET application that calls web services on another host. Is
there a way that you can control the lifetime of the underlying TCP
connections that .NET uses to make the web service calls? From what I
understand, by default, .NET is configured to use a maximum of two
connections, and those connections stay open for up to 100 seconds after the
last data was sent/received. Is there a way to control the lifetime of
those underlying connections? In other words, can I have .NET use a
different connection for each request, or, time out the existing connection
quickly, so that it needs to create a new connection more often than the 100
second default timeout? Thanks in advance.
 
C

CaffieneRush

You can set the Timeout property on the web service proxy instance
before you make a call to the web method.

Dim ws As WebServiceA
ws.Timeout = 10 * 1000 * 60 '10 minutes

Dim myResult As Integer

Try
myResult = ws.GetMyResult()

'Do something with myResult

Catch wex As WebException

'Check and handle the web exception

End Try

The web service client will now wait 10 minutes before timing out.
If you want the client to wait indefinitely (maybe for debugging) then
set the Timeout to -1

HTH
Andy
 
S

Scott Baierl

Thank you. I'll give that a try.


You can set the Timeout property on the web service proxy instance
before you make a call to the web method.

Dim ws As WebServiceA
ws.Timeout = 10 * 1000 * 60 '10 minutes

Dim myResult As Integer

Try
myResult = ws.GetMyResult()

'Do something with myResult

Catch wex As WebException

'Check and handle the web exception

End Try

The web service client will now wait 10 minutes before timing out.
If you want the client to wait indefinitely (maybe for debugging) then
set the Timeout to -1

HTH
Andy
 
S

Scott Baierl

That sets the amount of time that a webservice call waits for the completion
of a synchronous webservice call. That's not what I need. What I need is a
way to control how long the underlying TCP connection is cached or kept open
after the call has completed.
 
R

Ramya A

I think this can be possible by utilizing a timer in your ASP page for
a set interval.
Eveytime the interval laspses, it comes back to the timer and calls the
webservice again if the response results are not yet received.
You can also use waitforresponse property on the HTTP request object
and check for readystate of the HTTP request object in addition to the
timer.

Hope this helps.
Ramya
 
S

Scott Baierl

I found the solution to my problem. It involves adding the following
override to my proxy class:

protected override System.Net.WebRequest.GetWebRequest(Uri uri)
{
System.Net.Http.WebRequest webRequest =
(System.Net.Http.WebRequest)base.GetWebRequest(uri);
webRequest.KeepAlive = false;
return webRequest;
}

This code forces .NET to create a new connection for each web service
request, closing the connection when the request has finished.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top