<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.microsoft.co.il/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Ido Flatow&amp;#39;s Blog&lt;h3&gt;Veni Vidi Scripsi&lt;/h3&gt; : ServicePoint.ConnectionLimit</title><link>http://blogs.microsoft.co.il/blogs/idof/archive/tags/ServicePoint.ConnectionLimit/default.aspx</link><description>Tags: ServicePoint.ConnectionLimit</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>ServicePointManager.DefaultConnectionLimit == 2? The Depends continues…</title><link>http://blogs.microsoft.co.il/blogs/idof/archive/2011/06/28/servicepointmanager-defaultconnectionlimit-2-the-depends-continues.aspx</link><pubDate>Tue, 28 Jun 2011 15:32:42 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:846883</guid><dc:creator>Ido Flatow</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/idof/rsscomments.aspx?PostID=846883</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/idof/archive/2011/06/28/servicepointmanager-defaultconnectionlimit-2-the-depends-continues.aspx#comments</comments><description>&lt;p&gt;A few days ago I posted about the missing documentation about &lt;a href="http://blogs.microsoft.co.il/blogs/idof/archive/2011/06/20/servicepointmanager-defaultconnectionlimit-2-depends.aspx"&gt;ServicePointManager.DefaultConnectLimit&lt;/a&gt; and that its default value is not always 2 (the value actually depends on the target server, if it’s a remote server or your localhost).&lt;/p&gt;  &lt;p&gt;Yesterday, after my &lt;a href="http://blogs.microsoft.co.il/blogs/idof/archive/2011/06/27/my-wcf-crash-course-and-advanced-workshop-in-sela-s-dev-days.aspx"&gt;advanced WCF course&lt;/a&gt;, I got an email from one of the attendees that pointed out that indeed the default value is 2 for remote servers, but only in desktop applications (console, win forms…). In WCF services that are hosted in a web application, the default value for outgoing connections is actually 12 times the number of cores.&lt;/p&gt;  &lt;p&gt;First of all, thanks Miron for noticing this behavior of web applications, and I’ll just want to add some more information about this setting in web environemnts:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The change in the default value comes from System.Web.HttpRuntime, meaning it will be for any web hosted application, such as ASP.NET, WCF, and ASMX Web Services. &lt;/li&gt;    &lt;li&gt;The documentation in &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx"&gt;MSDN&lt;/a&gt; indeed mentions a changed setting with ASP.NET: &lt;em&gt;“When used in the server environment (ASP.NET) DefaultConnectionLimit defaults to higher number of connections, which is 10&lt;/em&gt;”.       &lt;br /&gt;As you can see, this documentation is also outdated… &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Hope you can use this information for better configuring your desktop clients, web applications, and services (self-hosted, or IIS hosted).&lt;/p&gt;  &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fidof%2farchive%2f2011%2f06%2f28%2fservicepointmanager-defaultconnectionlimit-2-the-depends-continues.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fidof%2farchive%2f2011%2f06%2f28%2fservicepointmanager-defaultconnectionlimit-2-the-depends-continues.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=846883" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/Dev+days/default.aspx">Dev days</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/ServicePoint.ConnectionLimit/default.aspx">ServicePoint.ConnectionLimit</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/ServicePointManager.DefaultConnectionLimit/default.aspx">ServicePointManager.DefaultConnectionLimit</category></item><item><title>ServicePointManager.DefaultConnectionLimit == 2? Depends…</title><link>http://blogs.microsoft.co.il/blogs/idof/archive/2011/06/20/servicepointmanager-defaultconnectionlimit-2-depends.aspx</link><pubDate>Mon, 20 Jun 2011 14:39:42 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:844294</guid><dc:creator>Ido Flatow</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/idof/rsscomments.aspx?PostID=844294</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/idof/archive/2011/06/20/servicepointmanager-defaultconnectionlimit-2-depends.aspx#comments</comments><description>&lt;p&gt;“Depends” is my favorite answer when teaching .NET-related courses, because behind this simple word lies the implementation of the CLR and BCL, which sometimes lead us to answers we didn’t expect.&lt;/p&gt;  &lt;p&gt;For example, take the question in the topic: according to &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx"&gt;MSDN&lt;/a&gt;, the default value of System.Net.ServicePointManager.DefaultConnectionLimit is 2. So I wrote some code that tries to open a lot of connections from a console application to a WCF service, and to my amaze, I was able to send dozens of requests at a time, without hitting the two connection limit. &lt;/p&gt;  &lt;p&gt;So I used my debugging skills, and found out that although the default value for the connection limit was 2, the ServicePoint.ConnectionLimit for my connection was set to 2147483647 – I didn’t expect that!&lt;/p&gt;  &lt;p&gt;If you look at the MSDN page for &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.servicepoint.connectionlimit.aspx"&gt;ServicePoint.ConnectionLimit&lt;/a&gt; you can clearly see that it says: “&lt;em&gt;The value of the ConnectionLimit property is set to the value of the ServicePointManager.DefaultConnectionLimit property when the &lt;/em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.net.servicepoint.aspx"&gt;&lt;em&gt;ServicePoint&lt;/em&gt;&lt;/a&gt;&lt;em&gt; object is created&lt;/em&gt;”. If the DefaultConnectionLimit has the default value of 2, that should have also set the ConnectionLimit value to 2, or am I wrong? I’m probably wrong thinking it’s that obvious.&lt;/p&gt;  &lt;p&gt;Therefore, I went to reflector to see what I was doing wrong, and after searching around a bit, I found the following implementation in the ServicePoint.ConnectionLimit get property:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; get_ConnectionLimit()
{
  &lt;span class="kwrd"&gt;if&lt;/span&gt; ((!&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_UserChangedLimit &amp;amp;&amp;amp; (&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_IPAddressInfoList == &lt;span class="kwrd"&gt;null&lt;/span&gt;)) &lt;br /&gt;  &amp;amp;&amp;amp; (&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_HostLoopbackGuess == TriState.Unspecified))
  {
    &lt;span class="kwrd"&gt;lock&lt;/span&gt; (&lt;span class="kwrd"&gt;this&lt;/span&gt;)
    {
      &lt;span class="kwrd"&gt;if&lt;/span&gt; ((!&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_UserChangedLimit &amp;amp;&amp;amp; (&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_IPAddressInfoList == &lt;br /&gt;         &lt;span class="kwrd"&gt;null&lt;/span&gt;)) &amp;amp;&amp;amp; (&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_HostLoopbackGuess == TriState.Unspecified))
      {
        IPAddress address = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (IPAddress.TryParse(&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_Host, &lt;span class="kwrd"&gt;out&lt;/span&gt; address))
        {
          &lt;span class="kwrd"&gt;this&lt;/span&gt;.m_HostLoopbackGuess = &lt;br /&gt;            IsAddressListLoopback(&lt;span class="kwrd"&gt;new&lt;/span&gt; IPAddress[] { address }) &lt;br /&gt;            ? TriState.True : TriState.False;
        }
        &lt;span class="kwrd"&gt;else&lt;/span&gt;
        {
          &lt;span class="kwrd"&gt;this&lt;/span&gt;.m_HostLoopbackGuess = &lt;br /&gt;            NclUtilities.GuessWhetherHostIsLoopback(&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_Host) ? &lt;br /&gt;            TriState.True : TriState.False;
        }
      }
    }
  }
  &lt;span class="kwrd"&gt;if&lt;/span&gt; (!&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_UserChangedLimit &amp;amp;&amp;amp; &lt;br /&gt;  !((&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_IPAddressInfoList == &lt;span class="kwrd"&gt;null&lt;/span&gt;) ? &lt;br /&gt;  (&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_HostLoopbackGuess != TriState.True) : &lt;br /&gt;  !&lt;span class="kwrd"&gt;this&lt;/span&gt;.m_IPAddressesAreLoopback))
  {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; 0x7fffffff;
  }
  &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;.m_ConnectionLimit;
}&lt;/pre&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

&lt;p&gt;If you look at the above code, you will notice something that pops at you right away – loopback.&lt;/p&gt;

&lt;p&gt;Apparently the property checks if you are trying to access your own machine before deciding which limit to use. In my case I was connecting to my machine, and I ended up getting the value of 0x7fffffff which translates to 2147483647 – which is what I saw while debugging.&lt;/p&gt;

&lt;p&gt;I went and made a quick change, found an available server, installed my testing service in it, changed the address, and voila – the ServicePoint.ConnectionLimit returned 2, and I could see that only two connections were executing at any given time.&lt;/p&gt;

&lt;p&gt;Now let’s go back to the question I asked and ask it again:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is ServicePointManager.DefaultConnectionLimit == 2? &lt;/strong&gt;It depends:&lt;/p&gt;

&lt;p&gt;1. If you are calling a remote service, then yes, it will be 2 by default&lt;/p&gt;

&lt;p&gt;2. If you are calling to your local machine, then no, the default will be Int32.MaxValue&lt;/p&gt;

&lt;p&gt;For best performance, the recommendation is to set the ServicePointManager.DefaultConnectionLimit to 12 times the number of cores in your client machine (for example, I have an 8 core machine, therefore 12*8=96 connections).&lt;/p&gt;

&lt;p&gt;“Depends”, I love that answer.&lt;/p&gt;

&lt;p&gt;By the way, next week we’re having our &lt;a href="http://www.sela.co.il/s/minisdp/index.html"&gt;DevDays&lt;/a&gt; at Sela, and I’m going to give an advanced course in WCF. If you thought this anecdote was interesting, come and learn more about some hidden features and settings of WCF. I promise lots of fun and a lot of “it depends” answers.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=844294" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/.NET+4/default.aspx">.NET 4</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/default+value+2/default.aspx">default value 2</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/ServicePoint.ConnectionLimit/default.aspx">ServicePoint.ConnectionLimit</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/ServicePointManager.DefaultConnectionLimit/default.aspx">ServicePointManager.DefaultConnectionLimit</category></item></channel></rss>