如何从Polly重试更改HttpMessageHandler的配置? [英] How I can change configuration of HttpMessageHandler from Polly retry?

查看:74
本文介绍了如何从Polly重试更改HttpMessageHandler的配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题.我有一个代理集.如果通过代理的请求非常缓慢或崩溃,我想在没有代理的情况下重试.

对于设置代理,我在 Startup.cs 文件中具有以下代码:

  services.AddHttpClient< ICheckPackagesService,CheckPackagesService>(x =>{x.Timeout = TimeSpan.FromSeconds(10);} .ConfigurePrimaryHttpMessageHandler(()=>新的SocketsHttpHandler{代理=新的WebProxy(IpService.GetIp())}); 

但是我无法想象在没有代理的情况下发送一个新请求(如果前一个请求当然崩溃了),我该怎么做.在官方文档中进行快速研究不会给我带来任何好处.

请分享您在这种情况下的经验.谢谢!

P.S.我使用 .NET Core 2.2.401 .

解决方案

无法更改 HttpClientHandler <的任何属性/code>,或在实例化后将新版本的 HttpClientHandler 分配给现有的 HttpClient .

因此,您不能使用通过HttpClientFactory配置的Polly重试策略通过新代理进行调用(通过HttpClientFactory配置的Polly策略为第一个链接中所述,请使用在IHttpClientFactory上定义的命名客户端,并为每个代理端点定义一个命名客户端.

I have the following issue. I have a proxy set. If a request via proxy very slow or has crashed I would like to try again without proxy.

For setting proxy I have the following code in the Startup.cs file:

services.AddHttpClient<ICheckPackagesService, CheckPackagesService>(x =>
{
    x.Timeout = TimeSpan.FromSeconds(10);
}).ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler
{
    Proxy = new WebProxy(IpService.GetIp())
});

But I can't imagine what I have to do for sending new one request without proxy (if the previous one has crashed of course!). A fast research in the official documentation gives me nothing.

Please, share your experience for this case. Thank you!

P.S. I use .NET Core 2.2.401.

解决方案

You cannot change the any of the properties of HttpClientHandler or assign a new version of HttpClientHandler to an existing HttpClient after it is instantiated.

Therefore you cannot use a Polly retry policy configured via HttpClientFactory to call via a new proxy (Polly policies configured via HttpClientFactory are applied as a DelegatingHandler within HttpClient).

As stated in the first link, use named clients defined on IHttpClientFactory instead, and define a named client for each proxy endpoint.

这篇关于如何从Polly重试更改HttpMessageHandler的配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆