views:

1363

answers:

4

Those "fine" RFCs mandate from every RFC-client that they beware of not using more than 2 connections per host...

Microsoft implemented this in WebClient. I know that it can be turned off with

App.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
 <system.net> 
  <connectionManagement> 
   <add address="*" maxconnection="100" /> 
  </connectionManagement> 
 </system.net> 
</configuration>

(found on http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/1f863f20-09f9-49a5-8eee-17a89b591007 )

But how can I do it programmatically?

Accordin to http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx

"Changing the DefaultConnectionLimit property has no effect on existing ServicePoint objects; it affects only ServicePoint objects that are initialized after the change. If the value of this property has not been set either directly or through configuration, the value defaults to the constant DefaultPersistentConnectionLimit."

I'd like best to configure the limit when I instanciate the WebClient, but just removing this sad limitation programmatically at the start of my programm would be fine, too.

The server I access is not a regular webserver in the internet, but under my control and in the local lan. I want to do API-calls, but I don't use webservices or remoting

+1  A: 

If you find the ServicePoint object being used by your WebClient, you can change its connection limit. HttpWebRequest objects have an accessor to retrieve the one they were constructed to use, so you could do it that way. If you're lucky, all your requests might end up sharing the same ServicePoint so you'd only have to do it once.

I don't know of any global way to change the limit. If you altered the DefaultConnectionLimit early enough in execution, you'd probably be fine.

Alternately, you could just live with the connection limit, since most server software is going to throttle you anyway. :)

Kevin Gadd
This server will not throttle me (in fact, it will, but in a different way) as it is completely under my control
Christian
+7  A: 

With some tips from here and elsewhere I managed to fix this in my application by overriding the WebClient class I was using:

class AwesomeWebClient : WebClient {
    protected override WebRequest GetWebRequest(Uri address) {
     HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(address);
     req.ServicePoint.ConnectionLimit = 10;
     return (WebRequest)req;
    }
}
Shizam
A: 

We have a situation regarding the above piece of configuration in App.Config

In order for this to be valid in a CONSOLE Application, we added the System.Configuration reference dll. Without the reference, the above was useless.

Teo-Kostas
A: 

for those interested:

System.Net.ServicePointManager.DefaultConnectionLimit = x (where x is your desired number of connections)

no need for extra references

just make sure this is called BEFORE the service point is created as mentioned above in the post.

lilmoe