FluentRetry 2.1.2
dotnet add package FluentRetry --version 2.1.2
NuGet\Install-Package FluentRetry -Version 2.1.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FluentRetry" Version="2.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentRetry" Version="2.1.2" />
<PackageReference Include="FluentRetry" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FluentRetry --version 2.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: FluentRetry, 2.1.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FluentRetry@2.1.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FluentRetry&version=2.1.2
#tool nuget:?package=FluentRetry&version=2.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
FluentRetry
A lightweight, fluent retry helper for C#. Sensible defaults, minimal ceremony.
Why FluentRetry?
- ✅ Simple: Two entry points
Retry.Do(...)/Retry.DoAsync(...)cover all cases - ⚙️ Configurable: Attempts, delay, exponential backoff, jitter, result- or exception-based retry
- 📦 Presets:
Fast(),Standard(),Resilient(),Network(),Database()for one‑liners - 🔁 Unified model: Same fluent builder for actions and functions (sync & async)
- 🧪 Tested: Comprehensive unit coverage
Install
dotnet add package FluentRetry
Quick Examples
using FluentRetry;
// Action with defaults (3 attempts, 150ms delay)
Retry.Do(() => CallService()).Execute();
// Function with return value
var data = Retry.Do(() => Fetch()).Execute();
// Async function
var dto = await Retry.DoAsync(async () => await client.GetAsync(url))
.Network() // preset: 4 attempts, base 100ms, exponential, jitter
.ExecuteAsync();
Fluent Configuration
await Retry.DoAsync(async () => await UnstableCall())
.Attempts(5) // default: 3
.Delay(200) // ms, default: 150
.WithExponentialBackoff() // doubles delay each retry (capped internally)
.WithJitter(50) // add 0..50ms random jitter (default: 50)
.OnRetry((ex, attempt) => Console.WriteLine($"Attempt {attempt} failed: {ex.Message}"))
.OnFailure(ex => Console.WriteLine($"All attempts failed: {ex.Message}"))
.ThrowOnFailure() // rethrow final failure (off by default)
.ExecuteAsync();
Retry Based on Result
var result = Retry.Do(() => TryGetValue())
.Attempts(5)
.RetryWhen(v => v == null || v == 0)
.Execute();
Extension Shortcuts
// Action
action.WithRetry();
action.WithRetry(5);
// Function
var value = func.WithRetry();
var other = await asyncFunc.WithRetryAsync();
// Exponential
action.WithExponentialRetry();
var payload = await asyncFunc.WithExponentialRetryAsync(attempts: 4, baseDelayMs: 100);
Presets
| Preset | Intent | Settings |
|---|---|---|
Fast() |
Very quick operations | 2 attempts, 50ms, jitter 25ms |
Standard() |
Balanced default | 3 attempts, 150ms, jitter 50ms |
Resilient() |
Unstable deps | 5 attempts, 500ms, jitter 200ms |
Network() |
HTTP / flaky IO | 4 attempts, 100ms, exponential, jitter 50ms |
Database() |
DB timeouts | 3 attempts, 1000ms, jitter 200ms |
Usage:
Retry.Do(() => Work()).Resilient().Execute();
var entity = await Retry.DoAsync(async () => await repo.Load())
.Database()
.ExecuteAsync();
Global Defaults
Retry.SetGlobalDefaults(attempts: 5, delayMs: 250);
// Subsequent builders pick up these defaults
Retry.Do(() => Work()).Execute();
(Existing builders keep the defaults captured at creation time.)
API Surface
| Category | Methods |
|---|---|
| Entry | Retry.Do(Action), Retry.Do<T>(Func<T>), Retry.DoAsync(Func<Task>), Retry.DoAsync<T>(Func<Task<T>>) |
| Core config | .Attempts(int), .Delay(int), .WithExponentialBackoff(), .WithJitter(int) |
| Behavior | .ThrowOnFailure(bool=true) |
| Callbacks | .OnRetry(Action<Exception,int>), .OnFailure(Action<Exception>) |
| Result-based | .RetryWhen(Func<T,bool>) (generic only) |
| Presets | .Fast(), .Standard(), .Resilient(), .Network(), .Database() |
| Extensions | action.WithRetry(), func.WithRetry(), action.WithExponentialRetry(), ...Async variants |
| Globals | Retry.SetGlobalDefaults(int attempts, int delayMs) |
Typical Scenarios
HTTP (with backoff)
var body = await Retry.DoAsync(async () => {
var resp = await client.GetAsync(url);
resp.EnsureSuccessStatusCode();
return await resp.Content.ReadAsStringAsync();
})
.Network()
.ThrowOnFailure()
.ExecuteAsync();
Database
var rows = await Retry.DoAsync(async () => await db.QueryAsync<User>(sql))
.Database()
.ExecuteAsync();
File IO (transient locks)
var text = Retry.Do(() => File.ReadAllText(path))
.Fast()
.Execute();
Error Semantics
- If all attempts fail and
ThrowOnFailure()is NOT set, the last exception is suppressed (actions) or last result is returned (generic). For result retries that never satisfy the condition andThrowOnFailure()is set, anInvalidOperationExceptionis thrown. - Cancellation (
OperationCanceledException) is never retried and is rethrown immediately.
Design Notes
- No external dependencies
- Thread-safe global defaults; individual builders are not intended for concurrent Execute calls
- Jitter adds 0..N ms uniformly to mitigate thundering herd
- Exponential backoff uses doubling with an internal safety cap
Contributing
PRs welcome. Please include tests for behavioral changes.
License
MIT © Contributors
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.