2

我正在调用外部 API,并且我想重试某些特定响应(例如BadGateway、Unauthorized、ServiceUnavailable)。第二次重试后,它说

任务取消(“任务已取消。”)为什么?

我正在尝试使用Polly以下代码:

try
{
    await Policy.HandleResult<HttpResponseMessage>(r => _reTryHttpStatusCodes.Contains(r.StatusCode))
        .WaitAndRetryAsync(new[] {
            TimeSpan.FromSeconds(1),
            TimeSpan.FromSeconds(2),
            TimeSpan.FromSeconds(4)
        })
        .ExecuteAsync(async () =>
        {
            responseMessage = await base.SendAsync(request, cancellationToken);
            return responseMessage;
        }
        );
    return null;
}
catch (Exception ex)
{
    if (IsTransientException(ex))
    {
        throw new HttpRequestException("Exception Occured", ex);
    }
    throw;
}


    private readonly HttpStatusCode[] _reTryHttpStatusCodes =
        new[]
        {
            HttpStatusCode.BadGateway,
            HttpStatusCode.GatewayTimeout,
            HttpStatusCode.ServiceUnavailable,
            HttpStatusCode.RequestTimeout,
            HttpStatusCode.Unauthorized
        };
4

0 回答 0