Polly重试除特定条件外的所有异常

时间:2018-06-20 12:47:51

标签: c# polly

Polly中是否有一种方法可以重试除指定的例外之外的所有例外。例如:

var p = Policy
    .Handle<HttpListenerException>(e => !(e.NativeErrorCode == 1))
    .Or<Exception>()
    .RetryAsync();

在这里,我选择了一种稍微有些人为的情况,想在NativeErrorCode == 1重试吗?

最初,我希望如果.Or<Exception>() ..

处理了除1以外的任何其他值,则将重试此操作。

实际上发生了什么,即使.Or<Exception>也被NativeErrorCode == 1捕获了,即使它被从上面排除了吗?我认为。

我考虑过但未经测试的一个选项...(无错误检查; p)

var p = Policy
    .Handle<Exception>(e => SomethingMoreComplex(e) == true)
    .RetryAsync();

private bool SomethingMoreComplex(Exception e)
{
    if (e is HttpListenerException t)
    {
        if (t.NativeErrorCode == 1) return false;
    }

    return true;
}

那是线程安全的吗? :|

1 个答案:

答案 0 :(得分:1)

如果您查看 Policy.HandleSyntax.cs 文件,您会看到 Handle<T> 方法是如何定义的:

public partial class Policy
{
    public static PolicyBuilder Handle<TException>() where TException : Exception
        => new PolicyBuilder(exception => exception is TException ? exception : null);

    public static PolicyBuilder Handle<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception
        => new PolicyBuilder(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null);
        
    ...
}

public partial class Policy<TResult>
{
    public static PolicyBuilder<TResult> Handle<TException>() where TException : Exception
        => new PolicyBuilder<TResult>(exception => exception is TException ? exception : null);

    public static PolicyBuilder<TResult> Handle<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception
        => new PolicyBuilder<TResult>(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null);
            
    ...
}
  • 所有方法都依赖于 PolicyBuilder 类,该类具有 internal ctor
    • 因此,我们无法在自定义代码中使用它。
  • 还要注意 Policydoes not a public ctor
    • 因此,我们无法创建 Policy 实例,这就是为 Policy 类创建扩展方法没有意义的原因。

以下是克服这些限制的一种方法:

public static class PolicyExt
{
    public static PolicyBuilder HandleExcept<TException>() where TException : Exception
        => Policy.Handle((Exception exception) => exception is TException is false);

    public static PolicyBuilder HandleExcept<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception
        => Policy.Handle((Exception exception) => exception is TException is false || exception is TException texception && !exceptionPredicate(texception));
}

public static class PolicyExt<TResult>
{
    public static PolicyBuilder<TResult> Handle<TException>() where TException : Exception
        => Policy<TResult>.Handle((Exception exception) => exception is TException is false);

    public static PolicyBuilder<TResult> Handle<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception
        => Policy<TResult>.Handle((Exception exception) => exception is TException is false || exception is TException texception && !exceptionPredicate(texception));
}

最后是一个快速测试:

var policy = PolicyExt.HandleExcept<Exception>(ex => ex is NotSupportedException)
    .WaitAndRetry(2, _ => TimeSpan.FromSeconds(2));

//Or just:
//var policy = PolicyExt.HandleExcept<NotSupportedException>()
//    .WaitAndRetry(2, _ => TimeSpan.FromSeconds(2));

policy.Execute(() =>
{
    Console.WriteLine("Have been called");
    throw new NotSupportedException();
});

输出:

Have been called
Unhandled exception. System.NotSupportedException: Specified method is not supported.
  • 因此,如果 NotSupportedException 未触发重试逻辑。
  • 但如果我们针对以下委托执行政策:
policy.Execute(() =>
{
    Console.WriteLine("Have been called");
    throw new Exception("Custom");
});

然后输出如下:

Have been called
Have been called
Have been called
Unhandled exception. System.Exception: Custom

因此,重试被触发。