Port exhaustion with HttpClient

Topics: Web Api
Jun 17, 2011 at 2:35 PM

I'm calling my REST service using the HttpClient class, wrapping the code in a using e.g.

using (var client = new HttpClient(uri))
     var response = client.Get();
     // Do somthing

What I'm finding is that I quite often get a low-level error:

ServiceUnavailable - Unable to connect to the remote server Only one usage of each socket address (protocol/network address/port) is normally permitted

I think this is due to the TCP port timeout issue, where the underlying port is not released for a number of seconds after the port is closed.

Couple of questions...

  1. 1. Has anyone else encountered this
  2. 2. Is there a way of pooling connections used by HttpClient; I've done low-level socket stuff before so I know .NET is capable of much better behaviour