HttpClient是否安全并发使用?

时间:2022-05-25 06:59:41

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.

在我能找到的所有HttpClient的用法示例中,它用于一个off调用。但是,如果我有一个持久的客户端情况,在这种情况下可以并发地发出多个请求,该怎么办呢?基本上,打电话给客户安全吗?针对HttpClient的相同实例,在两个线程上同时执行PostAsync。

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.

我并不是在寻找实验结果。作为一个工作示例,它可能只是一个侥幸(而且是一个持久的例子),失败的示例可能是一个错误的配置问题。理想情况下,我正在寻找一些关于HttpClient中并发处理问题的权威答案。

3 个解决方案

#1


107  

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

根据MSDN,由于。net 4.5,以下实例方法是线程安全的(谢谢@ischell):

CancelPendingRequests
DeleteAsync
GetAsync
GetByteArrayAsync
GetStreamAsync
GetStringAsync
PostAsync
PutAsync
SendAsync

#2


85  

Here is another article from Henrik F. Nielsen about HttpClient where he says:

下面是另一篇来自Henrik F. Nielsen关于HttpClient的文章,他说:

"The default HttpClient is the simplest way in which you can start sending requests. A single HttpClient can be used to send as many HTTP requests as you want concurrently so in many scenarios you can just create one HttpClient and then use that for all your requests."

默认的HttpClient是开始发送请求的最简单的方式。一个HttpClient可以用来同时发送任意数量的HTTP请求,所以在许多场景中,您可以创建一个HttpClient,然后将其用于所有请求。

#3


14  

Found one MSDN forum post by Henrik F. Nielsen (one of HttpClient's principal Architects).

找到一篇来自Henrik F. Nielsen (HttpClient的主要架构师之一)的MSDN论坛帖子。

Quick summary:

快速总结:

  • If you have requests that are related (or won't step on eachother) then using the same HttpClient makes a lot of sense.
  • 如果您有相关的请求(或者不愿相互执行),那么使用相同的HttpClient就很有意义。
  • In genral I would recommend reusing HttpClient instances as much as possible.
  • 在genral中,我建议尽可能重用HttpClient实例。

#1


107  

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

根据MSDN,由于。net 4.5,以下实例方法是线程安全的(谢谢@ischell):

CancelPendingRequests
DeleteAsync
GetAsync
GetByteArrayAsync
GetStreamAsync
GetStringAsync
PostAsync
PutAsync
SendAsync

#2


85  

Here is another article from Henrik F. Nielsen about HttpClient where he says:

下面是另一篇来自Henrik F. Nielsen关于HttpClient的文章,他说:

"The default HttpClient is the simplest way in which you can start sending requests. A single HttpClient can be used to send as many HTTP requests as you want concurrently so in many scenarios you can just create one HttpClient and then use that for all your requests."

默认的HttpClient是开始发送请求的最简单的方式。一个HttpClient可以用来同时发送任意数量的HTTP请求,所以在许多场景中,您可以创建一个HttpClient,然后将其用于所有请求。

#3


14  

Found one MSDN forum post by Henrik F. Nielsen (one of HttpClient's principal Architects).

找到一篇来自Henrik F. Nielsen (HttpClient的主要架构师之一)的MSDN论坛帖子。

Quick summary:

快速总结:

  • If you have requests that are related (or won't step on eachother) then using the same HttpClient makes a lot of sense.
  • 如果您有相关的请求(或者不愿相互执行),那么使用相同的HttpClient就很有意义。
  • In genral I would recommend reusing HttpClient instances as much as possible.
  • 在genral中,我建议尽可能重用HttpClient实例。