0

我指的是这个 https://github.com/Pathoschild/FluentHttpClient#custom-retry--coordination来创建自定义重试协调并尝试在此处使用 Polly,但我遇到了以下错误,

“PolicyBuilder<HttpResponseMessage>”不包含“重试”的定义,并且最佳扩展方法重载“RetrySyntax.Retry(PolicyBuilder, int, Action)”需要“PolicyBuilder”类型的接收器

这里有什么问题?

public class PollyCoordinator : IRequestCoordinator
{
    public Task<HttpResponseMessage> ExecuteAsync(IRequest request, Func<IRequest, Task<HttpResponseMessage>> dispatcher)
    {
        int[] retryCodes = { 408, 500, 502, 503, 504 };
        return Policy
            .HandleResult<HttpResponseMessage>(r => retryCodes.Contains((int)r.StatusCode))
            .Retry(3, async () => await dispatcher(request));
    }
}
4

1 回答 1

1

我更新了 FluentHttpClient 自述文件中的示例。这是您的代码的固定版本:

public class PollyCoordinator : IRequestCoordinator
{
    public Task<HttpResponseMessage> ExecuteAsync(IRequest request, Func<IRequest, Task<HttpResponseMessage>> send)
    {
        int[] retryCodes = { 408, 500, 502, 503, 504 };
        return Policy
            .HandleResult<HttpResponseMessage>(r => retryCodes.Contains((int)r.StatusCode)) // should we retry?
            .RetryAsync(3) // up to 3 retries
            .ExecuteAsync(() => send(request)); // begin handling request
    }
}

如果您希望每次重试之间有延迟(推荐),您可以RetryAsync(3)用类似WaitAndRetryAsync(3, attempt => TimeSpan.FromSeconds(attempt)).

于 2019-11-16T16:47:57.287 回答