Connection Management


When retrieving multiple URIs, it is often the case that many of the resources reside on the same host. If an application needs to retrieve hundreds or thousands of URIs, the application should limit the number of concurrent HTTP requests . This allows efficient use of network bandwidth and lessens the load on the Web server. To accomplish this, the .NET Framework offers the ServicePointManager and ServicePoint classes for managing HTTP requests.

The ServicePointManager class defines limits for a destination, such as the maximum number of concurrent connections allowed at any given time. The first step for managing Web connections is to establish the limits on the ServicePointManager that will be applied to subsequent connection requests. Table 10-5 lists the important properties of the ServicePointManager class.

Table 10-5: ServicePointManager Properties

Property

Description

DefaultConnectionLimit

Maximum number of concurrent connections to a single ServicePoint

MaxServicePointIdleTime

Maximum time in milliseconds a ServicePoint can be idle before being garbage collected

MaxServicePoints

Maximum number of concurrent ServicePoint objects

Once the restrictions are set on the ServicePointManager , a ServicePoint object is retrieved for a given server before any Web requests are issued to that server. A ServicePoint is retrieved by calling FindServicePoint . If a ServicePoint already exists from a previous request, the same object is returned; otherwise , a new ServicePoint is created so long as the MaxServicePoints limit is not exceeded. The following code illustrates this:

C#

 ServicePointsp; GlobalProxySelection.Select=newWebProxy(http://myproxy:80/); ServicePointManager.MaxServicePoints=2; ServicePointManager.DefaultConnectionLimit=2; ServicePointManager.MaxServicePointIdleTime=20000;//20seconds sp=ServicePointManager.FindServicePoint(newUri(http://www.one.com)); //GetUriResourceretrievesaURI GetUriResource(http://www.one.com/one.html); sp=ServicePointManager.FindServicePoint(newUri(http://www.two.com)); GetUriResource(http://www.two.com/two.html) 

Visual Basic .NET

 DimspAsServicePoint GlobalProxySelection.Select=NewWebProxy("http://myproxy:80/") ServicePointManager.MaxServicePoints=2 ServicePointManager.DefaultConnectionLimit=2 ServicePointManager.MaxServicePointIdleTime=2000020seconds sp=ServicePointManager.FindServicePoint(NewUri("http://www.one.com")) 


Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net