我使用以下代码配置了 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}");
});
这作为单例注入到类中并按以下方式使用:
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);
重试时会出现此问题,原始网址 https://someurl.com/post/2 现在变为 https://someurl.com/post/22 (重复了两个)并且这种情况不断发生每次重试。例如,第 10 次重试的 URL 将是 https://someurl.com/post/222222222
任何想法将不胜感激。
根据定义重试re-执行提供的用户委托/用户回调。这意味着它不会代表您更改请求中的任何内容。
例如,如果您想在基于 HttpClient 的网络请求中更改请求 url,那么您必须编写一段代码,以便在每次重试策略调用用户委托时获取新的 url。 在这里您可以找到示例代码。
因此,
_apiClient.Post.Delete(2)
疑似的工作方式是将提供的数字附加在存储的请求网址的末尾。
有些人喜欢这个伪代码:
this.RequestUrl = $"{this.RequestUrl}{deleteParameter}";
为了解决这个问题(如果您可以更改底层代码),您必须将数字附加到 baseUrl 以构造请求 url。
this.RequestUrl = $"{this.BaseUrl}{deleteParameter}";