[英]Should a single HttpClientHandler be used with multiple HttpClient objects?
I have multiple long-lived HttpClient
objects connecting to different endpoints. 我有多个连接到不同端点的长期HttpClient
对象。 They use the same AutomaticDecompression setting. 他们使用相同的AutomaticDecompression设置。 Should I create a separate HttpClientHandler
for each one, or is it fine to share the handler instance between them all? 我应该为每个对象创建一个单独的HttpClientHandler
还是在所有对象之间共享处理程序实例?
HttpClientHandler httpHandler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
HttpClient offerClient = new HttpClient(httpHandler, true);
offerClient.DefaultRequestHeaders.Add("Accept-Content", "gzip");
HttpClient quoteClient = new HttpClient(httpHandler, true);
quoteClient.DefaultRequestHeaders.Add("Accept-Content", "gzip");
HttpClient ordersClient = new HttpClient(httpHandler, true);
ordersClient.DefaultRequestHeaders.Add("Accept-Content", "gzip");
Conventional wisdom has it that a singleton HttpClient
is the way to go, so sharing the same handler is certainly OK. 传统观点认为,使用单例HttpClient
是HttpClient
的,因此共享同一处理程序当然可以。 Depending on how your DI is configured, I would be tempted to create a GZipHttpClient
deriving from HttpClient
and just pass the correctly-configured handler straight to the base
ctor, and then wire that up as a singleton and request that specific Type (as opposed to HttpClient
) via DI wherever you need this gzip stuff. 根据您的DI的配置方式,我很想创建一个从HttpClient
派生的GZipHttpClient
,然后将正确配置的处理程序直接传递给base
ctor,然后将其连接为单例并请求该特定Type(相对于HttpClient
)可以通过DI在任何需要此gzip的地方使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.