If you look at the Policy.HandleSyntax.cs file then you can see how the Handle<T>
methods have been defined:
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);
...
}
- All of the methods rely on the
PolicyBuilder
class, which has an internal ctor.
- So, we can't use it in our custom code.
- Also notice that the
Policy
class does not have a public ctor.
- So, we can't create a
Policy
instance, that's why it does not make sense to create extension methods for the Policy
class.
Here is one way to overcome on these limitations:
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));
}
And finally here is a quick test:
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();
});
Output:
Have been called
Unhandled exception. System.NotSupportedException: Specified method is not supported.
- So, in case
NotSupportedException
the retry logic is not triggered.
- But if we execute the policy against the following delegate:
policy.Execute(() =>
{
Console.WriteLine("Have been called");
throw new Exception("Custom");
});
Then the output will be the following:
Have been called
Have been called
Have been called
Unhandled exception. System.Exception: Custom
So, the retry is triggered.
.Handle<HttpListenerException>(e => e.NativeErrorCode != 1).Or<Exception>(e => !(e is HttpListenerException))
(that is, simply excludeHttpListenerException
from the most inclusive check). The shortest way is probably.Handle<Exception>(e => (e as HttpListenerException)?.NativeErrorCode != 1)
, but that's unnecessarily cryptic. – DinerSomethingMoreComplex(...)
predicate isn't sharing any state outside the scope that could make it non-thread-safe. So, LGTM. – Gann