pClientHandler与HttpClientFactory

pClientHandler与HttpClientFactory

本文介绍了如何在.NET Core中将HttpClientHandler与HttpClientFactory一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用.NET Core 2.1中提供的 HttpClientFactory ,但我也想使用 HttpClientHandler 在创建 HttpClients 时利用 AutomaticDecompression 属性。

I want to use the HttpClientFactory that is available in .NET Core 2.1 but I also want to use the HttpClientHandler to utilize the AutomaticDecompression property when creating HttpClients.

我正在努力,因为 .AddHttpMessageHandler<> 接受的是 DelegatingHandler 而不是 HttpClientHandler

I am struggling because the .AddHttpMessageHandler<> takes a DelegatingHandler not a HttpClientHandler.

有人知道如何使它工作吗?

Does anyone know how to get this to work?

谢谢,
Jim

Thanks,Jim

推荐答案

实际上我不是在使用自动解压缩,但是要实现此目的的方法是正确注册http客户端

Actually I'm not using automatic decompression but the way to achieve this is to properly register http client

services.AddHttpClient<MyCustomHttpClient>()
   .ConfigureHttpMessageHandlerBuilder((c) =>
     new HttpClientHandler()
     {
        AutomaticDecompression = System.Net.DecompressionMethods.GZip
     }
   )
   .AddHttpMessageHandler((s) => s.GetService<MyCustomDelegatingHandler>())

这篇关于如何在.NET Core中将HttpClientHandler与HttpClientFactory一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:58