简体   繁体   English

我应该对每个功能使用HttpClient还是对整个应用程序使用HttpClient?

[英]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.

相关问题 我应该在HttpClient中使用Using语句吗? - Should I use the Using statement with HttpClient? 我应该如何在 HttpClient 中使用证书? - How should I use a certificate with HttpClient? 自定义数据库连接对象:我应该对每个对象实例使用一个通用对象(单个)还是一个? - Custom Database Connection Object: Should i use a general one (singleton) or one per object instance? 数据库触发器或应用程序代码? 在这种情况下,我应该使用哪一个? - Database trigger or application code? which one should I use in this case? 我应该使用OwinContext的环境来保存每个请求的特定于应用程序的数据 - Should I use OwinContext's Environment to hold application specific data per request 我应该使用“每种类型的表”还是“每种层次的表”? - Should I use Table Per Type or Table Per Hierarchy? 并非按页面构建整个Web应用程序 - Build whole web application not per page 对于Winform应用程序,我应如何在另一台计算机上使用一台SQL计算机的数据库? - how should i use database of one sql machine in an another machine for my winform application? Pushlet,长轮询或轮询-在聊天应用程序中应该使用哪一个? - Pushlet, Long polling or polling - Which one should I use in my chat application? 我应该在控制台应用程序中使用析构函数还是IDisposable - Should I use destructor or IDisposable in Console application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM