Skip to content

Pragmatic.Resilience

Native, AOT-safe resilience for .NET 10 — strategy composition, a fluent builder, DI integration, and source-generator support. No Polly dependency.

Distributed systems fail: calls time out, databases go down, APIs error. The usual fix — Polly policies or hand-rolled retry loops — scatters resilience across the codebase, pulls in external dependencies, and treats every failure the same, whether it’s transient (a network blip) or permanent (a validation error).

// Without Pragmatic: manual composition, repeated at every call site
var combined = Policy.WrapAsync(
Policy.TimeoutAsync(10),
Policy.Handle<HttpRequestException>().CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)),
Policy.Handle<HttpRequestException>().WaitAndRetryAsync(3, a => TimeSpan.FromMilliseconds(200 * Math.Pow(2, a))));
await combined.ExecuteAsync(ct => http.PostAsJsonAsync("/charges", request, ct), ct);

Declare a policy name; the generator composes the pipeline. One attribute, several strategies, zero manual wiring — and it distinguishes transient exceptions (retry/break) from Result failures (validation, not-found), which pass through unchanged.

[DomainAction]
[ResiliencePolicy("payment-gateway")]
public partial class ChargeCustomerAction : DomainAction<PaymentResult>
{
public override async Task<Result<PaymentResult, IError>> Execute(CancellationToken ct)
{
// wrapped by the "payment-gateway" pipeline automatically:
// retry + circuit breaker + timeout on exceptions; Result failures pass through
var response = await _http.PostAsJsonAsync("/charges", _request, ct);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<PaymentResult>(ct)!;
}
}
Terminal window
dotnet add package Pragmatic.Resilience

Strategies — retry (with backoff + jitter), circuit breaker, timeout, hedging, fallback, rate limiter, bulkhead — compose in a defined pipeline order; configure named policies in Program.cs or via the fluent builder. See Policies.

The strategy set, pipeline composition, DI integration, and the [ResiliencePolicy] generator are functional within the 0.8 preview. See the roadmap.

| Concepts | Strategy model, pipeline order, transient vs permanent failures | | Getting Started | Declare a policy, wrap an action/call | | Policies | Every strategy, parameters, configuration, named policies | | Common Mistakes | The most frequent resilience pitfalls | | Troubleshooting | Problem/solution guide |

  • .NET 10.0+

Part of the Pragmatic.Design ecosystem — see Licensing. Pragmatic.Resilience is MIT-licensed.