Search code examples
.netc#-4.0concurrencydotnet-httpclient

Is HttpClient safe to use concurrently?


In all the examples I can find of usages of HttpClient, it is used for one off calls. But what if I have a persistent client situation, where several requests can be made concurrently? Basically, is it safe to call client.PostAsync on 2 threads at once against the same instance of HttpClient.

I am not really looking for experimental results here. As a working example could simply be a fluke (and a persistent one at that), and a failing example can be a misconfiguration issue. Ideally I'm looking for some authoritative answer to the question of concurrency handling in HttpClient.


Solution

  • According to Microsoft Docs, since .NET 4.5 The following instance methods are thread safe (thanks @ischell):

    CancelPendingRequests
    DeleteAsync
    GetAsync
    GetByteArrayAsync
    GetStreamAsync
    GetStringAsync
    PostAsync
    PutAsync
    SendAsync
    PatchAsync