We all know that every applications sometime or other gives exception.
Have you ever wondered wish you could give Retry, Retry Forever or Wait and Retry on your particular exception? Well we have already a nuget package for that which is called Polly.
Polly is a .NET 3.5 / 4.0 / 4.5 / PCL library that allows developers to express transient exception handling policies such as Retry, Retry Forever, Wait and Retry or Circuit Breaker in a fluent manner.
First of all we will see how to install Polly. Well there is not any rocket science for it and we will simply do it as we install other nugget packages as shown below :-
Now I will summarise what facilities Polly provides us.
Handling exceptions :-
Handling exceptions can be a hassle sometimes. Not just setting the try/catches up, but deciding on the policy for the catch can make the exception management code more complex than the method itself!
So in Polly we can simply handle the exception as shown below :-
Policy.Handle<ArgumentNullException>();
and if you want to handle more exceptions then simply add .Or as shown below :-
Policy
.
Handle
<ArgumentNullException>()
.Or<DivideByZeroException>();
Policy
.Handle<ArgumentNullException>(ex => ex.ParamName == "test");
// Retry once
Policy.Handle<ArgumentNullException>().Retry();// Retry multiple times
Policy.Handle<ArgumentNullException>().Retry(3);
Policy
.Handle<ArgumentNullException>()
.Retry(3, (exception, count) =>
{
// do something
});
Policy.Handle<ArgumentNullException>().RetryForever();
Policy
.Handle<ArgumentNullException>()
.RetryForever(exception =>
{
// do something
});
Policy
.Handle<ArgumentNullException>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
});
Policy
.Handle<ArgumentNullException>()
.WaitAndRetry(new[]
{
1.Seconds(),
2.Seconds(),
3.Seconds()
}, (exception, timeSpan) => {
// do something
});
Policy
.Handle<ArgumentNullException>()
.CircuitBreaker(2, TimeSpan.FromMinutes(1));
Once you have set up a policy, you execute on it.
Policy
.Handle<SqlException>(ex => ex.Number == 1205)
.Or<ArgumentException>(ex => ex.ParamName ==
"example"
)
.Retry()
.Execute(() => DoSomething());
Asynchronous Support (.NET 4.5 Only) :-
You can use Polly with asynchronous functions by using the asynchronous methods
RetryAsync
RetryForeverAsync
WaitAndRetryAsync
CircuitBreakerAsync
ExecuteAsync
In place of their synchronous counterparts
Retry
RetryForever
WaitAndRetry
CircuitBreaker
Execute
For example :-
await Policy
.Handle<SqlException>(ex => ex.Number == 1205)
.Or<ArgumentException>(ex => ex.ParamName == “example“)
.RetryAsync()
.ExecuteAsync(() => DoSomethingAsync());
I hope it will be useful and let me know if you want improvement because this is my first blog and by this blog I have started my journey of blogging!
Happy coding!
Neel, have you tried using Polly with ASP.NET Core at all? I wasn’t too sure if your post was in the context of ASP.NET “classic” or ASP.NET Core. I’m not too sure if Polly supports ASP.NET Core yet.
Thanks!
LikeLike
Hey Michael, No I have not tried using Polly with .Net Core yet and above post does not include .Net core. But I guess Polly supports .Net core from 2.2.4. Have a look here: https://www.nuget.org/packages/Polly/2.2.4
LikeLike