Does WebClient Keep Connection Open?

J

Jules Winfield

Dear Friend,

My WPF client application makes use of a proprietary HTTP-based API via the
WebClient class. It needs to make a large number of calls to this API. Each
call must be made in succession. At no time is more than one call being made
at the same time. Each call instantiates a *new* WebClient object and calls
the appropriate methods to obtain the response from the HTTP server.

My question is: How many connections are being made to the server "under the
hood?" Is a separate connection being established for each call (that is,
each new instantiation of WebClient) and then being shut down after the
response is received? Or alternatively, is a single connection being
established, kept alive, and then reused for each call? I'd certainly prefer
the latter. The number of calls to the API will be so large that
establishing a new TCP/IP HTTP connection for each call would seem to be an
inefficient use of resources.

Jules
 
S

Steven Cheng

Hi Jules,

As for the WebClient, it works the same way like other system.net
webrequest based components(such as HttpWebRequest). The Webclient or
HttpWebRequest instance you created is not necessarily to be the underlying
physical network connection. .NET will has an additional "ServicePoint"
layer that will help maintain the physical network connection(TCP
socket...).

Also, there are many properties and configuration settings may help you
customize the underlying network behavior. Here are some good articles
explain some of the underying processing of webrequest based component(same
for WebClient):

#Understanding System.Net Connection Management and ServicepointManager
http://blogs.msdn.com/adarshk/pages/345411.aspx

#Connection Groups and Connection Limits
http://blogs.msdn.com/mflasko/archive/2005/09/11/463865.aspx

BTW, .net application has a default limitation of 2 connections to a given
remote server. That means by default only two physical network connection
can be established to the same remote server at the same time. This can be
controlled via the following configuration element:

#ConnectionManagementElement..::.MaxConnection Property
http://msdn2.microsoft.com/en-us/library/system.net.configuration.connection
managementelement.maxconnection.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day 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 or complex
project analysis and dump analysis issues. 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/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
 
J

Jules Winfield

Thanks, Steven... Your explanation and the referenced articles answered all
of my questions!

JUles
 

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

Top