带有最终异常的 Polly WaitAndRetry 不执行任何操作
Polly WaitAndRetry with final exception does nothing
我执行对不太稳定的外部服务的调用,因此抛出 WebExceptions。
我想重试几次,在最后一次尝试后我想抛出收到的最后一个错误。
这是我对 Polly (v6.1.1) 的尝试:
public static Policy WaitAndRetryPolicy<T>(short nrOfRetryAttempts = 5) where T : Exception
{
var waitAndRetry = Policy
.Handle<T>()
.WaitAndRetry(nrOfRetryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
var fallbackForLastError = Policy
.Handle<T>()
.Fallback(
fallbackAction: () => { },
onFallback: (ex) => { throw ex; });
return Policy.Wrap(fallbackForLastError, waitAndRetry);
}
来电者,遗留 VB.Net:
Dim retryPolicy = Policies.WaitAndRetryPolicy(Of WebException)()
Dim theResult = retryPolicy.
ExecuteAndCapture(Function()
Return aProxy.GetSomething(a, b)
End Function).Result
当我 运行 上面描述的代码时,theResult 保持为空并且似乎没有调用服务。
如果我只是使用没有 Fallback 函数的 WaitAndRetryPolicy,该服务被调用并且重试机制按预期工作(当然不会抛出异常)。
我怎样才能实现我的目标,而不必在调用方代码中检查 PolicyResult.FinalException?
我不知道最后一个异常,但我已经使用 Retry 和 CircuitBreakerException(带包装)实现了非常相似的行为。所以可以尝试3次,失败2次后抛出circuitbreakerexception。然后你就可以对最后一个异常做出反应。
Policy
.Handle<SomeExceptionType>()
.CircuitBreaker(2, TimeSpan.FromMinutes(x));
要让 Polly 重新抛出任何最终异常,而不是将其捕获到 PolicyResult.FinalException
,只需使用 .Execute(...)
或 .ExecuteAsync(...)
重载而不是 .ExecuteAndCapture(...)
执行策略或 .ExecuteAndCaptureAsync(...)
我执行对不太稳定的外部服务的调用,因此抛出 WebExceptions。
我想重试几次,在最后一次尝试后我想抛出收到的最后一个错误。
这是我对 Polly (v6.1.1) 的尝试:
public static Policy WaitAndRetryPolicy<T>(short nrOfRetryAttempts = 5) where T : Exception
{
var waitAndRetry = Policy
.Handle<T>()
.WaitAndRetry(nrOfRetryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
var fallbackForLastError = Policy
.Handle<T>()
.Fallback(
fallbackAction: () => { },
onFallback: (ex) => { throw ex; });
return Policy.Wrap(fallbackForLastError, waitAndRetry);
}
来电者,遗留 VB.Net:
Dim retryPolicy = Policies.WaitAndRetryPolicy(Of WebException)()
Dim theResult = retryPolicy.
ExecuteAndCapture(Function()
Return aProxy.GetSomething(a, b)
End Function).Result
当我 运行 上面描述的代码时,theResult 保持为空并且似乎没有调用服务。 如果我只是使用没有 Fallback 函数的 WaitAndRetryPolicy,该服务被调用并且重试机制按预期工作(当然不会抛出异常)。
我怎样才能实现我的目标,而不必在调用方代码中检查 PolicyResult.FinalException?
我不知道最后一个异常,但我已经使用 Retry 和 CircuitBreakerException(带包装)实现了非常相似的行为。所以可以尝试3次,失败2次后抛出circuitbreakerexception。然后你就可以对最后一个异常做出反应。
Policy
.Handle<SomeExceptionType>()
.CircuitBreaker(2, TimeSpan.FromMinutes(x));
要让 Polly 重新抛出任何最终异常,而不是将其捕获到 PolicyResult.FinalException
,只需使用 .Execute(...)
或 .ExecuteAsync(...)
重载而不是 .ExecuteAndCapture(...)
执行策略或 .ExecuteAndCaptureAsync(...)