cjablonski76 cjablonski76 - 1 month ago 16
C# Question

C# Does Each Instance of HttpClient Get it's Own ServicePoint

I have something like the following

public async Task PostTest()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("abc.com");
var response = await client.PostAsync("/api/call", new StringContent("hello world"));
//do something with response and dispose it.
//NOTE: server is long running, so dispose is not getting called before sending other remaning requests.
}
}


I in turn call this method from a loop of to send a large number of requests at in quick succession.

Based on what I have read about
HttpClient
it will associate requests to a specific ServicePoint object, which will have a default ConnectionLimit value of 2. Since all of my requests are to the same uri, only one
ServicePoint
object should be created, and therefore limit me to two maximum concurrent requests (assume pipelining is off).

What I actually see when running this is that if I call my
PostTest()
method at the "same time" n times, I see n number of requests getting to the server before any response is ever sent back since server's api logic is long running.

Can someone explain why this example seems to be exceeding the ServicePointManager.DefaultConnectionLimit of 2? My best guess is that since I am creating a new
HttpClient
per request the
ServicePointManager
is making a
ServicePoint
object per request, but from what I have read I thought
ServicePointManager
should only create different instances of
ServicePoint
per unique schema and domain, so I am unsure.

NOTE: I do plan on reusing my
HttpClient
instance, but this outcome peaked my curiosity :).

Answer

Just in case anyone else is curious, I inspected the HttpClient code a little bit, and it looks like each HttpClient instantiates an HttpMessageHandler per instance. The HttpMessageHandler object creates a hash code per instance which gets used to create ServicePoint objects, in addition to the schema and domain of the uri defined by the HttpClient.