英文:
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<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() {}
));
}
I need to add the Polly policies to the client. How can I do this when creating a client with RestService.For<>
?
答案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<XYZ>()
.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<IMyService>(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论