我已经使用以下代码配置了Polly IAsyncPolicy:
IAsyncPolicy retryPolicy = Policy
.Handle<HttpRequestException>(e => !e.IsBadRequest())
.WaitAndRetryAsync(
Backoff.DecorrelatedJitterBackoffV2("00:00:01", 5),
onRetry: (response, timeSpan, retryCount, context) =>
{
if (!context.TryGetLogger(out var logger))
{
return;
}
logger.LogWarning($"Retrying call {retryCount} with timespan: {timeSpan} due to: {response.Message}");
});
它作为Singleton注入到类中,并以以下方式使用:
var context = new Context($"API-call-{Guid.NewGuid()}", new Dictionary<string, object>
{
{ "logger", _logger },
});
await _resiliencePolicy.ExecuteAsync(async ctx => await _apiClient.Post.Delete(2), _context);
问题发生在进行重试时,原始URL https://someurl.com/post/2现在变成https://someurl.com/post/22(重复两次),并且每次重试都会继续发生这种情况.比方说,第10次重试的URL为https://someurl.com/post/222222222
任何 idea 都将受到极大的赞赏.