如何使用WaitandRetry并执行最终错误?

发布于 2025-01-28 03:01:47 字数 757 浏览 3 评论 0原文

我试图检查一个简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2025-02-04 03:01:47

您不必明确重新启动最后一个例外。

重试以下方式工作:

  • 如果用装饰的方法抛出异常,并且有一个相关的.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:

  • If an exception is thrown by the decorated method and there is a related .Handle<TEx> or .Or<TEx> clause inside the policy definition then it checks if the retry limit has been reached or not
    • If the retry limit has not been exceeded then it will perform yet another retry attempt
    • If the retry limit has been reached then it will throw the last exception
  • If there is not related .Handle<TEx> or .Or<TEx> clause then it will throw the last exception

Here is a diagram from the official documentation
retry logic


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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文