Polly is a .NET 3.5 / 4.0 / 4.5 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.
Install-Package Polly
Policy
.Handle<DivideByZeroException>()Policy
.Handle<SqlException>(ex => ex.Number == 1205)Policy
.Handle<DivideByZeroException>()
.Or<ArgumentException>()Policy
.Handle<SqlException>(ex => ex.Number == 1205)
.Or<ArgumentException>(ex => ex => x.ParamName == "example")Policy
.Handle<DivideByZeroException>()
.Retry()Policy
.Handle<DivideByZeroException>()
.Retry(3)Policy
.Handle<DivideByZeroException>()
.Retry(3, (exception, retyCount) =>
{
// do something
});Policy
.Handle<DivideByZeroException>()
.RetryForever()Policy
.Handle<DivideByZeroException>()
.RetryForever(exception =>
{
// do something
});Policy
.Handle<DivideByZeroException>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
});Retry, waiting a specified duration between each retry, calling an action on each retry with the current exception and duration
Policy
.Handle<DivideByZeroException>()
.WaitAndRetry(new[]
{
1.Seconds(),
2.Seconds(),
3.Seconds()
}, (exception, timeSpan) => {
// do something
}); Retry a specified number of times, using a function to calculate the duration to wait between retries based on the current retry attempt (allows for exponential backoff)
// in this case will wait for
// 1 ^ 2 = 2 seconds then
// 2 ^ 2 = 4 seconds then
// 3 ^ 2 = 8 seconds then
// 4 ^ 2 = 16 seconds then
// 5 ^ 2 = 32 seconds
Policy
.Handle<DivideByZeroException>()
.WaitAndRetry(5, retryAttempt =>
TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)
);Retry a specified number of times, using a function to calculate the duration to wait between retries based on the current retry attempt, calling an action on each retry with the current exception and duration
Policy
.Handle<DivideByZeroException>()
.WaitAndRetry(
5,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
(exception, timeSpan) => {
// do something
}
);Circuit Breaker, break circuit after the specified number of exceptions and keep circuit broken for the specified duration
Policy
.Handle<DivideByZeroException>()
.CircuitBreaker(2, TimeSpan.FromMinutes(1));For more information on the Circuit Breaker pattern see:
var policy = Policy
.Handle<DivideByZeroException>()
.Retry();
policy.Execute(() => DoSomething());var policy = Policy
.Handle<DivideByZeroException>()
.Retry();
var result = policy.Execute(() => DoSomething());Policy
.Handle<SqlException>(ex => ex.Number == 1205)
.Or<ArgumentException>(ex => ex.ParamName == "example")
.Retry()
.Execute(() => DoSomething());-
Fluent Assertions - A set of .NET extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style test | Microsoft Public License (Ms-PL)
-
xUnit.net - Free, open source, community-focused unit testing tool for the .NET Framework | Apache License 2.0 (Apache)
-
[Ian Griffith's TimedLock] (http://www.interact-sw.co.uk/iangblog/2004/04/26/yetmoretimedlocking) Acknowledgements =
-
lokad-shared-libraries - Helper assemblies for .NET 3.5 and Silverlight 2.0 that are being developed as part of the Open Source effort by Lokad.com (discontinued) | New BSD License
Licensed under the terms of the New BSD License
