【问题标题】:Action before retry重试前的操作
【发布时间】:2017-10-17 17:34:49
【问题描述】:

我有这个用例,对于一些错误,我需要在重试之前执行一个操作;否则,请重试。

类似这样的:

try
{
action:
    <action>
}
catch (SpecialException)
{
    <cleanup>
    goto action:
}
catch (Exception)
{
    goto action:
}

Polly 可以做到这一点吗?

【问题讨论】:

    标签: polly


    【解决方案1】:

    Retry(Action&lt;Exception, int&gt; onRetry)注册句柄,重试前会执行动作。这样你就可以在某些情况下进行清理。

    Policy.Handle<Exception>().Retry((ex, count) => {
        if(ex is NotImplementedException)
        {
            Console.WriteLine("clear up");
        }
        }).Execute(() => {
            Console.WriteLine("throw exception");
            throw new Exception();//or NotImplementedException
        });
    

    【讨论】:

    • 是的,完全正确。 Polly 中的onRetry 委托挂钩也可用于各种重试策略(重试;重试 n 次;永远重试;等待并重试)。并且还以异步形式提供用于异步执行,作为 onRetryAsync: 委托挂钩。 Doco,示例:github.com/App-vNext/Polly/#retry
    猜你喜欢
    • 2022-08-17
    • 1970-01-01
    • 2022-06-14
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多