如何在 .NET Core 中使用 HttpClientHandler

我想使用的HttpClientFactory是在.NET 2.1核心可用,但我也想用HttpClientHandler,以利用AutomaticDecompression创建时属性HttpClients

我很挣扎,因为.AddHttpMessageHandler<>需要一个DelegatingHandler不是HttpClientHandler.

有谁知道如何让这个工作?


哈士奇WWW
浏览 359回答 2
2回答

白板的微信

其实我没有使用自动解压,但实现这一点的方法是正确注册http客户端services.AddHttpClient<MyCustomHttpClient>()&nbsp; &nbsp;.ConfigureHttpMessageHandlerBuilder((c) =>&nbsp; &nbsp; &nbsp;new HttpClientHandler()&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; AutomaticDecompression = System.Net.DecompressionMethods.GZip&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;)&nbsp; &nbsp;.AddHttpMessageHandler((s) => s.GetService<MyCustomDelegatingHandler>())

ITMISS

通过 HttpClientBuilder 的 ConfigurePrimaryHttpMessageHandler() 方法定义主 HttpMessageHandler 更合适。请参阅下面的示例以配置类型化客户端。services.AddHttpClient<TypedClient>()&nbsp; &nbsp; .ConfigureHttpClient((sp, httpClient) =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var options = sp.GetRequiredService<IOptions<SomeOptions>>().Value;&nbsp; &nbsp; &nbsp; &nbsp; httpClient.BaseAddress = options.Url;&nbsp; &nbsp; &nbsp; &nbsp; httpClient.Timeout = options.RequestTimeout;&nbsp; &nbsp; })&nbsp; &nbsp; .SetHandlerLifetime(TimeSpan.FromMinutes(5))&nbsp; &nbsp; .ConfigurePrimaryHttpMessageHandler(x => new HttpClientHandler()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;&nbsp; &nbsp; })&nbsp; &nbsp; .AddHttpMessageHandler(sp => sp.GetService<SomeCustomHandler>().CreateAuthHandler())&nbsp; &nbsp; .AddPolicyHandlerFromRegistry(PollyPolicyName.HttpRetry)&nbsp; &nbsp; .AddPolicyHandlerFromRegistry(PollyPolicyName.HttpCircuitBreaker);您还可以通过使用 Polly 库的特殊构建器方法来定义错误处理策略。在这个示例中,策略应该被预定义并存储到策略注册服务中。public static IServiceCollection AddPollyPolicies(&nbsp; &nbsp; this IServiceCollection services,&nbsp;&nbsp; &nbsp; Action<PollyPoliciesOptions> setupAction = null){&nbsp; &nbsp; var policyOptions = new PollyPoliciesOptions();&nbsp; &nbsp; setupAction?.Invoke(policyOptions);&nbsp; &nbsp; var policyRegistry = services.AddPolicyRegistry();&nbsp; &nbsp; policyRegistry.Add(&nbsp; &nbsp; &nbsp; &nbsp; PollyPolicyName.HttpRetry,&nbsp; &nbsp; &nbsp; &nbsp; HttpPolicyExtensions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HandleTransientHttpError()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .WaitAndRetryAsync(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; policyOptions.HttpRetry.Count,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retryAttempt => TimeSpan.FromSeconds(Math.Pow(policyOptions.HttpRetry.BackoffPower, retryAttempt))));&nbsp; &nbsp; policyRegistry.Add(&nbsp; &nbsp; &nbsp; &nbsp; PollyPolicyName.HttpCircuitBreaker,&nbsp; &nbsp; &nbsp; &nbsp; HttpPolicyExtensions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HandleTransientHttpError()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .CircuitBreakerAsync(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handledEventsAllowedBeforeBreaking: policyOptions.HttpCircuitBreaker.ExceptionsAllowedBeforeBreaking,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; durationOfBreak: policyOptions.HttpCircuitBreaker.DurationOfBreak));&nbsp; &nbsp; return services;}
打开App,查看更多内容
随时随地看视频慕课网APP