Polly中是否有一种方法可以重试除指定的例外之外的所有例外。例如:
var p = Policy
.Handle<HttpListenerException>(e => !(e.NativeErrorCode == 1))
.Or<Exception>()
.RetryAsync();
在这里,我选择了一种稍微有些人为的情况,想在NativeErrorCode == 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;
}
那是线程安全的吗? :|
答案 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。
Policy
类 does 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
因此,重试被触发。