如何在Refit中使用Polly?

huangapple go评论73阅读模式
英文:

How to use Polly with Refit?

问题

以下是翻译好的部分:

"我没有在 Startup 中使用服务来配置我的 Refit 客户端。我正在执行以下操作:

public Task<IMyService> GetService()
{
    var retryPolicy = HttpPolicyExtensions
        .HandleTransientHttpError()
        .Or<TimeoutRejectedException>()
        .WaitAndRetryAsync(2, _ => TimeSpan.FromMilliseconds(500));

    var timeoutPolicy = Policy
        .TimeoutAsync<HttpResponseMessage>(TimeSpan.FromMilliseconds(500));

    return Task.FromResult(RestService.For<IMyService>(new HttpClient((DelegatingHandler)myHandler)
    {
        BaseAddress = new Uri(myUrl)
    },
        new RefitSettings() {}
    ));
}

我需要将 Polly 策略添加到客户端。在使用 RestService.For<> 创建客户端时,我该如何做呢?"

英文:

I am not configuring my Refit client using the services in Startup. I am doing the following:

public Task&lt;IMyService&gt; GetService()
{
    var retryPolicy = HttpPolicyExtensions
        .HandleTransientHttpError()
        .Or&lt;TimeoutRejectedException&gt;()
        .WaitAndRetryAsync(2, _ =&gt; TimeSpan.FromMilliseconds(500));

    var timeoutPolicy = Policy
        .TimeoutAsync&lt;HttpResponseMessage&gt;(TimeSpan.FromMilliseconds(500));

    return Task.FromResult(RestService.For&lt;IMyService&gt;(new HttpClient((DelegatingHandler)myHandler)
    {
        BaseAddress = new Uri(myUrl)
    },
        new RefitSettings() {}
    ));
}

I need to add the Polly policies to the client. How can I do this when creating a client with RestService.For&lt;&gt;?

答案1

得分: 0

通常情况下,每当您注册一个被装饰以Polly策略(或一系列策略)的命名/类型化的`HttpClient`时,您会这样做

.AddHttpClient<XYZ>()
.AddPolicyHandler(...);

`AddPolicyHandler` [注册][1] 一个带有指定Polly策略的`PolicyHttpMessageHandler`。 ([ref][2])

所以,您可以在这里做同样的事情:

var combinedPolicy = Policy.WrapAsync(retryPolicy, timeoutPolicy);
var outerHandler = new PolicyHttpMessageHandler(combinedPolicy);
outerHandler.InnerHandler = (DelegatingHandler)myHandler;

然后,您可以在`RestService.For` 调用中使用`outerHandler`

RestService.For<IMyService>(new HttpClient(outerHandler))

*我还没有测试过,但运行时可能需要您将最内部的处理程序设置为一个新的`HttpClientHandler`才能正常工作。*

[1]: https://github.com/dotnet/aspnetcore/blob/a450cb69b5e4549f5515cdb057a68771f56cefd7/src/HttpClientFactory/Polly/src/DependencyInjection/PollyHttpClientBuilderExtensions.cs#L31
[2]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.pollyhttpclientbuilderextensions.addpolicyhandler?view=dotnet-plat-ext-3.1
英文:

Usually whenever you register a named/typed HttpClient which is decorated with a Polly policy (or chain of policies) then you do that like this

.AddHttpClient&lt;XYZ&gt;()
.AddPolicyHandler(...); 

The AddPolicyHandler registers a PolicyHttpMessageHandler with the specified Polly policy. (ref)

So, you can do the same here:

var combinedPolicy = Policy.WrapAsync(retryPolicy, timeoutPolicy);
var outerHandler = new PolicyHttpMessageHandler(combinedPolicy);
outerHandler.InnerHandler = (DelegatingHandler)myHandler;

Then you can use the outerHandler in your RestService.For call

RestService.For&lt;IMyService&gt;(new HttpClient(outerHandler))

I haven't tested it but the runtime might require you to set the most inner handler to a new HttpClientHandler to work properly.

huangapple
  • 本文由 发表于 2023年8月4日 22:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定