Wednesday, 10 July 2013

HttpWebRequest Connection limitation


As you may know, by default, the System.Net implementation of C# is using connection pool to improving performance which respects restrictions stated in http 1.1 - one user agent SHOULD NOT create more than 2 connection to a server. This causes max 2 live connections can be created by HttpWebRequest in the same app domain at any time. So the wordaround can be just host the code in different app domains and fire the method in multi threads.

Or, you can change app configuration to rewrite the default connection limit:
<system.net>
     <connectionManagement>
         <add address="*" maxcoonection="2000" />
     </connectionManagement>
</system.net>

Or, you can rewrite the limitation in your code before initialize the first HttpWebRequest:
ServicePointManager.DefaultConnectionLimit = 100;

No comments: