2

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 theServicePointManager 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 :).

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

and FYI here's some guidance for using HttpClient: makolyte.com/…
0

@cjablonski76 I believe your explanation is wrong. Because the creation of ServicePoint is as follows per the framework code.

            sp = new ServicePoint(address)
            {
                ConnectionLimit = DefaultConnectionLimit,
                IdleSince = DateTime.Now,
                Expect100Continue = Expect100Continue,
                UseNagleAlgorithm = UseNagleAlgorithm
            };
            s_servicePointTable[tableKey] = new WeakReference<ServicePoint>(sp);

If you see this, there is no usage of hashcode which support your statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.