Connection Groups and Connection Limits

The goal of this article is to explain how connection limits on service points are interpreted when connection groups are specified. Therefore, instead of repeating what MSDN already explains, I will provide pointers to the relevant MSDN doc sections that introduce service points and connection groups and dive right into the meat of this post.

What are Service Points?

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetservicepointclasstopic.asp

What are Connection Groups?

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetservicepointclassconnectionnametopic.asp

Specifying the # of concurrent connections for a service point (Connection Limit)

There are a few ways to set the connection limit for a service point. You can set this via a property on the ServicePointManager class (ServicePointManager.DefaultConnectionLimit) or via a configuration file as shown here:

 <configuration>
  <system.net>
   <connectionManagement>
    <add address="https://www.contoso.com" maxconnection="2" />
    <add address="192.168.1.2" maxconnection="4" />
    <add address="*" maxconnection="1" />
   </connectionManagement>
  </system.net>
 </configuration>

I recently received a question regarding how these settings are interpreted. For example, if I connect to www.contoso.com will the associated service point have a connection limit of 2 or 1? The answer is the most specific entry is the winner. Connecting to www.contoso.com will have a connection limit of 2, even if it resolves to 192.168.1.2.

If you want to verify this behavior you can use the following code:

using System;

using System.Net;

class Test

{

static void Main(String[] args)

{

            ServicePoint sp1 = ServicePointManager.FindServicePoint(new Uri("https://www.microsoft.com"));

            Console.WriteLine("Connection Limit (microsoft.com): {0}", sp1.ConnectionLimit);

            ServicePoint sp2 = ServicePointManager.FindServicePoint(new Uri("https://www.msdn.com"));

            Console.WriteLine("Connection Limit (msdn.com): {0}", sp2.ConnectionLimit);

}

}

If following config file is used:

<configuration>

 <system.net>

  <connectionManagement>

   <add address="https://www.microsoft.com" maxconnection="4" />

   <add address="192.168.1.2" maxconnection="4" />

   <add address="*" maxconnection="1" />

  </connectionManagement>

 </system.net>

</configuration>

Results:

Connection Limit (microsoft.com): 4

Connection Limit (msdn.com): 1

How do connection groups affect a service point’s connection Limit?

So now the million dollar question J - how do connection groups play into this? For example, assume 2 connection groups are present. Do the groups share the connection limit for an endpoint or do some other semantics apply? The answer is they do not share the connection limit, but, instead the connection limit is maintained per connection group. To illustrate this, assume I have the maximum connection limit set on the ServicePoint associated with the host Microsoft.com to 4. If I have two connection groups, each will have a connection limit of 4 when accessing Microsoft.com, thus enabling a maximum of 8 connections from my machine to Microsoft.com.