51
votes

I have a WCF service and a Web application. Web application makes calls to this WCF service in a continous manner a.k.a polling. In our production environment, I receive this error very rarely. Since, this is an internal activity users were not aware of when this error is thrown.

Could not connect to http://localhost/QAService/Service.svc. TCP error code 10048: Only one usage of each socket address (protocol/network address/port) is normally permitted 127.0.0.1:80. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted 127.0.0.1:80

I am having trouble in reproducing this behaviour in our dev/qa environment. I have made sure that the client connection is closed in a try..catch..finally block. Still don't understand what is causing this issue .. any one aware of this?

Note: I've looked at this SO question, but not seems to be answering my problem, so it is not repeated questions.

2
Is that because the web service is using port 80, which is in use by IIS? Which port does your service use in production? Which port, is IIS configured on in production?shahkalpesh
It is same 80 for both applications. WCF service is configured as a virtual directory inside the root in which the site is hosted. So, you're saying there could be occasionally a conflict between both web page request and a request to service from web page trying to use same port?asyncwait
I have encountered a similar situation. Planning to use net tcp port sharing feature in wcf service. msdn.microsoft.com/en-us/library/ms734772.aspx Please let me know if this worksuser272363

2 Answers

83
votes

You are overloading the TCP/IP stack. Windows (and I think all socket stacks actually) have a limitation on the number of sockets that can be opened in rapid sequence due to how sockets get closed under normal operation. Whenever a socket is closed, it enters the TIME_WAIT state for a certain time (240 seconds IIRC). Each time you poll, a socket is consumed out of the default dynamic range (I think its about 5000 dynamic ports just above 1024), and each time that poll ends, that particular socket goes into TIME_WAIT. If you poll frequently enough, you will eventually consume all of the available ports, which will result in TCP error 10048.

Generally, WCF tries to avoid this problem by pooling connections and things like that. This is usually the case with internal services that are not going over the internet. I am not sure if any of the wsHttp bindings support connection pooling, but the netTcp binding should. I would assume named pipes does not run into this problem. I couldn't say for the MSMQ binding.

There are two solutions you can use to get around this problem. You can either increase the dynamic port range, or reduce the period of TIME_WAIT. The former is probably the safer route, but if you are consuming an extremely high volume of sockets (which doesn't sound like the case for your scenario), reducing TIME_WAIT is a better option (or both together.)

Changing the Dynamic Port Range

  1. Open regedit.
  2. Open key HKLM\System\CurrentControlSet\Services\Tcpip\Parameters
  3. Edit (or create as DWORD) the MaxUserPort value.
  4. Set it to a higher number. (i.e. 65534)

Changing the TIME_WAIT delay

  1. Open regedit.
  2. Open key HKLM\System\CurrentControlSet\Services\Tcpip\Parameters
  3. Edit (or create as DWORD) the TcpTimedWaitDelay.
  4. Set it to a lower number. Value is in seconds. (i.e. 60 for 1 minute delay)

One of the above solutions should fix your problem. If it persists after changing the port range, I would see try increasing the period of your polling so it happens less frequently...that will give you more leeway to work around the time wait delay. I would change the time wait delay as a last resort.

11
votes

HttpClient, although it implements IDisposable is a shared object, you should reduce the number of instances as much as possible. You can get away with only having one instance for the entire lifetime of your application rather than one for each request.

I wrote about it pretty extensively at http://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/