System.Net.Sockets.SocketException under load

Topics: Web Api
Oct 11, 2011 at 11:46 PM

We are running into client socket exhaustion issue when making a large number of sequential calls to a Web API service using System.Net.Http.HttpClient.  I know that we can adjust TCP/IP socket connection timeout on OS level on the client but that doesn't really solve the issue but rather delay when it starts happening. :-)  Is there a way to have HttpClient pool connections for server-to-server communication?  If not, is it something that is considered for future releases of WCF Web API?

The exact error we are getting: System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted

Here is how we are calling our service:

HttpResponseMessage response;
using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var content = new ObjectContent<TRequest>(request, JsonMediaTypeFormatter.DefaultMediaType);
    response = client.Post(new Uri(baseAddress, relativeUri), content);
}