小编Mar*_*niz的帖子

Polly 策略无法使用“AddPolicyHandler”工作

我有一个应用程序请求经过身份验证的服务,需要通过access_token.

我的想法是在过期时使用 Polly 重试access_token

我在 .NET Core 3.1 应用程序中使用 Refit (v5.1.67) 和 Polly (v7.2.1)。

服务注册如下:

services.AddTransient<ExampleDelegatingHandler>();

IAsyncPolicy<HttpResponseMessage> retryPolicy = Policy<HttpResponseMessage>
    .Handle<ApiException>()
    .RetryAsync(1, (response, retryCount) =>
    {
        System.Diagnostics.Debug.WriteLine($"Polly Retry => Count: {retryCount}");
    });

services.AddRefitClient<TwitterApi>()
    .ConfigureHttpClient(c =>
    {
        c.BaseAddress = new Uri("https://api.twitter.com/");
    })
    .AddHttpMessageHandler<ExampleDelegatingHandler>()
    .AddPolicyHandler((sp, req) =>
    {
        //this policy does not works, because the exception is not catched on 
        //"Microsoft.Extensions.Http.PolicyHttpMessageHandler" (DelegatingHandler)
        return retryPolicy;
    });
Run Code Online (Sandbox Code Playgroud)
public interface TwitterApi
{
    [Get("/2/users")]
    Task<string> GetUsers();
}
Run Code Online (Sandbox Code Playgroud)
public class ExampleDelegatingHandler : DelegatingHandler …
Run Code Online (Sandbox Code Playgroud)

c# dotnet-httpclient refit .net-core polly

11
推荐指数
3
解决办法
2万
查看次数

标签 统计

.net-core ×1

c# ×1

dotnet-httpclient ×1

polly ×1

refit ×1