cjablonski76 cjablonski76 - 1 year ago 217
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
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
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
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
per request the
is making a
object per request, but from what I have read I thought
should only create different instances of
per unique schema and domain, so I am unsure.

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download