5

Polly 中是否有办法重试除指定异常之外的所有异常。例如:

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

在这里,我选择了一个稍微做作的情况,我不想在NativeErrorCode == 1?

我最初希望这将重试除 1 以外的任何值,以及由.Or<Exception>()..处理的任何其他异常。

实际发生的情况是,即使它被排除在上面,.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;
}

那是线程安全的吗?:|

4

1 回答 1

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具有内部 ctor的类。
    • 所以,我们不能在我们的自定义代码中使用它。
  • 另请注意,Policy该类不是公共 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

因此,触发了重试。

于 2021-02-15T14:43:48.300 回答