One of the deployment scenarios with DD4T 2.0 is to use a REST service. There are many advantages attached to this approach, but you have to be careful about one thing: the number of TCP connections between the web server and the REST server. In this article I will discuss the cause and provide a solution.
When you make a TCP connection from a Windows server to another server, there are 2 ports involved: an outbound port on the originating server and an inbound port on the destination. The inbound port on the REST service is always the same (usually port 80), but the outbound port is dynamically assigned by Windows.
The typical error message you find when you run into this problem, is this:
System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.
In other words: Windows has run out of free outgoing TCP ports, and is now trying to use one which is already in use.
Out of the box, when Windows needs to make an outgoing TCP connection, it picks a free port in the range 1024 – 5000. This means it has a total of 5000 minus 1024 = 3976 ports to choose from. That sounds like a large-enough number, but it may not be enough for your needs. Even though the ports are freed up again after a while, but if there are enough concurrent requests, you may run into the maximum pretty soon. On one occasion I found I only needed to simulate 15 concurrent visitors to make this happen.
That may sound odd, but you need to realize a typical DD4T web page can lead to dozens of requests:
- The page itself is 1 request
- There might be dynamic component presentations on the page, which could add 1 or 2 more
- If you use dynamic linking, each link leads to another request (add – say – 10 more requests)
- And finally, the page contains images and other binary files, which lead to additional requests (add another 10 or so).
This means that a single URL could easily lead to 25 requests or more. Normally, most of these calls are done only once; after the first time, the results will come from the cache. However, in some situations you may not have a cache, for example because the site was just restarted and the cache hasn’t filled up yet. Or perhaps you don’t want to use caching at all, for whatever reason.
In these cases, the default number of ports may not be enough. Fortunately, it is easy to increase the number. All you have to do is add a setting in the registry like this:
- Start Registry Editor
- Browse to, and then click the following key in the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters - On the Edit menu, click New, DWORD Value, and then add a registry key named ‘MaxUserPort’ with the desired value (it has to lie between 5000 and 65534)
- Make sure you enter the value as a decimal, not a hexadecimal number!
- Close Registry Editor
- Restart the server
That’s it. You are now able to handle many more visitor sessions with DD4T and REST!
More information: http://blogs.technet.com/b/tristank/archive/2008/03/11/maxuserport-what-it-is-what-it-does-when-it-s-important.aspx.