[英]Should I use HttpClient per function or one for whole application?
I'm creating .NET wrapper for some REST type API. 我正在为某些REST类型的API创建.NET包装器。 I'm using HttpClient for communication (sending/recieving Json).
我正在使用HttpClient进行通信(发送/接收Json)。 There are many async methods.
有许多异步方法。
Verification is via cookie that I'm receiving and storing in cookiecontainer. 验证是通过我正在接收并存储在cookiecontainer中的cookie进行的。 Should I have separate httpclient for every function or one global httpClient?
我应该为每个功能使用一个单独的httpclient还是一个全局httpClient? If one separate for each function, what is the best method to share cookiecontainer and custom headers that are attached to every request?
如果每个功能分开,共享每个请求附加的cookiecontainer和自定义标头的最佳方法是什么?
I suggest keep the number same as the API you're calling (this is how I coded mine), meaning if you're calling to Twitter API, just keep one instance of HttpClient for that, and if you're accessing Facebook API in the same time, keep another one for that. 我建议您保持与您要调用的API相同的编号(这就是我编码的方式),这意味着如果您要调用Twitter API,则只需为此保留一个HttpClient实例,并且如果要在其中访问Facebook API,同时,为此保留另一个。
Keep in mind that some metadata cannot be change after a HttpClient has been created (eg DefaultRequestHeaders), which make sense since a HttpClient is meant to be reuse to make multiple calls (eg easy for cookies or auth information sharing). 请记住,创建HttpClient之后,某些元数据将无法更改(例如DefaultRequestHeaders),这是有道理的,因为HttpClient用于重用以进行多次调用(例如,易于进行cookie或auth信息共享)。
Definitely per function, in a using
statement. 在
using
语句中,绝对是每个函数。 Custom headers can be stored in a static variable as long as they're only being read from. 只要只读取自定义标头,就可以将其存储在静态变量中。 The cookie container would likely require locking when working with any instance members.
当使用任何实例成员时,cookie容器可能需要锁定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.