如何使用WaitandRetry并执行最终错误?
我试图检查一个简单的WaitandRetry Polly
class Program
{
public static void Main()
{
int i = 0;
var _retryPolicy = Policy
.Handle<Exception>()
.WaitAndRetry(Backoff.ExponentialBackoff(TimeSpan.FromSeconds(2), 10),
(exception, timespan) =>
{
Console.WriteLine($"Retry: {timespan}. \n ex: {exception}");
});
_retryPolicy.Execute(() =>
{
Console.WriteLine(i);
i++;
int.Parse("something");
});
Console.ReadLine();
}
}
,并且在所有恢复失败后,我想提出最终的例外。我该怎么做?
例外的结果:
重试:..
重试:..
重试:..
我的新最终错误!
谢谢你!
I'm, trying to check a simple WaitAndRetry of Polly
class Program
{
public static void Main()
{
int i = 0;
var _retryPolicy = Policy
.Handle<Exception>()
.WaitAndRetry(Backoff.ExponentialBackoff(TimeSpan.FromSeconds(2), 10),
(exception, timespan) =>
{
Console.WriteLine(quot;Retry: {timespan}. \n ex: {exception}");
});
_retryPolicy.Execute(() =>
{
Console.WriteLine(i);
i++;
int.Parse("something");
});
Console.ReadLine();
}
}
And I want to throw a final exception after all the retries are failed. How can I do it?
Excepted Result:
Retry: ..
Retry: ..
Retry: ..
My new final error!
Thank You!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必明确重新启动最后一个例外。
重试以下方式工作:
.handle&lt; tex&gt;
或或- 如果尚未超过重试限制,则将执行另一次重试
- 如果达到了重试限制,则将抛出最后一个例外
.or&lt; tex&gt;
条款政策定义然后检查是否达到重试限制-retry-works“ rel =“ nofollow noreferrer”>官方文档

另外,请注意,如果您以调试模式运行应用程序,那么当装饰方法引发异常时,IDE可能会停止。这取决于您的IDE设置。
You don't have to explicitly re-throw the last exception.
The retry works in the following way:
.Handle<TEx>
or.Or<TEx>
clause inside the policy definition then it checks if the retry limit has been reached or not.Handle<TEx>
or.Or<TEx>
clause then it will throw the last exceptionHere is a diagram from the official documentation

Please also note that if you run your application in a debug mode then the IDE might stop each time when the decorated method throws exception. It depends on your IDE settings.