Failsafe is a very simple library that provides fluent interface for retrying an operation. It can be configured to catch an arbitrary set of exceptions using various patterns, with optional delay and retry limit. Works with synchronous and asynchronous methods.
- NuGet:
dotnet add package Failsafe
- Continuous integration
- Retry on specific, derived or any exception
- Match exceptions with a predicate
- Limit number of retries
- Configurable delay between retries
- Supports synchronous and asynchronous execution
- Fluent interface
- Targets .NET Framework 4.5+ and .NET Standard 2.0+
- No external dependencies
The following code executes FlakyMethod
, retrying on any exception until it finally succeeds.
var result = Retry.Create().CatchAnyException().Execute(FlakyMethod);
You can also configure which specific exceptions you want to retry on.
var result = Retry.Create()
// Catch any InvalidOperationException
.Catch<InvalidOperationException>()
// Catch IOException and derived from it
.Catch<IOException>(true)
// Catch HttpRequestException if the predicate matches
.Catch<HttpRequestException>(false, e => e.Message.Contains("403"))
.Execute(FlakyMethod);
It's possible to limit the number of retries and configure the delay between them.
var result = Retry.Create()
.CatchAnyException()
// Limit to 15 attempts
.WithMaxTryCount(15)
// Wait 0.2s after 1st, 0.4s after 2nd, etc.
.WithDelay(i => TimeSpan.FromSeconds(i*0.2))
.Execute(FlakyMethod);
If you really like my projects and want to support me, consider donating to me on Patreon or BuyMeACoffee. All donations are optional and are greatly appreciated. 🙏