HttpClient是否可以安全地同时使用? [英] Is HttpClient safe to use concurrently?
问题描述
在所有的例子中,我可以找到HttpClient的用法,它用于一个关闭调用。但是,如果我有一个持久的客户端情况,其中可以同时进行多个请求?基本上,是安全的调用client.PostAsync在两个线程一次对同一个HttpClient实例。
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.
我不是真的在这里寻找实验结果。作为一个工作示例可以简单地是一个fluke(和持久的那个),一个失败的示例可能是一个配置错误的问题。理想情况下,我正在寻找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.
推荐答案
根据MSDN ,自.NET 4.5以下实例方法是线程安全(感谢@ischell):
According to MSDN, since .NET 4.5 The following instance methods are thread safe (thanks @ischell):
CancelPendingRequests
DeleteAsync
GetAsync
GetByteArrayAsync
GetStreamAsync
GetStringAsync
PostAsync
PutAsync
SendAsync
这篇关于HttpClient是否可以安全地同时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!