NetMediate.Resilience
Adds retry, timeout and circuit-breaker pipeline behaviors to your NetMediate message pipeline.
Installation
dotnet add package NetMediate.Resilience
Registration
No manual registration is required. The source generator (NetMediate.SourceGeneration) automatically detects the NetMediate.Resilience assembly reference at compile time and registers all resilience behaviors before user-defined behaviors in the pipeline.
To customize the behavior options, use ConfigureOptions or Configure<T> before calling AddNetMediate():
// Override retry defaults (optional — defaults are applied automatically)
builder.Services.Configure<RetryBehaviorOptions>(opts =>
{
opts.MaxRetryCount = 3;
opts.Delay = TimeSpan.FromMilliseconds(200);
});
builder.Services.Configure<TimeoutBehaviorOptions>(opts =>
{
opts.RequestTimeout = TimeSpan.FromSeconds(10);
opts.NotificationTimeout = TimeSpan.FromSeconds(5);
});
builder.Services.Configure<CircuitBreakerBehaviorOptions>(opts =>
{
opts.FailureThreshold = 5;
opts.OpenDuration = TimeSpan.FromSeconds(30);
});
// The source generator emits this call — no manual call needed
builder.Services.AddNetMediate();
Each option is independent — override only the ones you need.
Behaviors
| Behavior | Registration service type | Applies to |
|---|---|---|
RetryRequestBehavior<TMessage, TResponse> | IPipelineRequestBehavior<,> | Request pipeline |
RetryNotificationBehavior<TMessage> | IPipelineBehavior<> | Notification pipeline |
TimeoutRequestBehavior<TMessage, TResponse> | IPipelineRequestBehavior<,> | Request pipeline |
TimeoutNotificationBehavior<TMessage> | IPipelineBehavior<> | Notification pipeline |
CircuitBreakerRequestBehavior<TMessage, TResponse> | IPipelineRequestBehavior<,> | Request pipeline |
CircuitBreakerNotificationBehavior<TMessage> | IPipelineBehavior<> | Notification pipeline |
All resilience behaviors follow the standard IPipelineBehavior<TMessage, TResult> contract, which means their Handle signature accepts object? key as the first parameter. The key is forwarded transparently so that keyed dispatch (e.g. mediator.Send("audit", command, ct)) passes the routing key through the full resilience pipeline.
Note: Notification handler dispatch is fire-and-forget by design — the
NotificationPipelineExecutorstarts all handlers simultaneously viaTask.WhenAlland discards their tasks. Handler exceptions and completion timing have no effect on the pipeline or the caller. Pipeline behaviors, however, run fully and their exceptions do propagate, so retry/timeout/circuit-breaker behaviors at the pipeline level protect the pipeline itself (e.g., adapter calls, pre-processing logic), not the individual handler execution.
Retry
Retries a failed pipeline step up to MaxRetryCount times with an optional delay between attempts.
builder.Services.Configure<RetryBehaviorOptions>(opts =>
{
opts.MaxRetryCount = 3;
opts.Delay = TimeSpan.FromMilliseconds(100);
});
Timeout
Cancels the request if it exceeds the configured timeout.
builder.Services.Configure<TimeoutBehaviorOptions>(opts =>
{
opts.RequestTimeout = TimeSpan.FromSeconds(30);
opts.NotificationTimeout = TimeSpan.FromSeconds(5);
});
Circuit Breaker
Opens the circuit after FailureThreshold consecutive failures and keeps it open for OpenDuration.
builder.Services.Configure<CircuitBreakerBehaviorOptions>(opts =>
{
opts.FailureThreshold = 5;
opts.OpenDuration = TimeSpan.FromMinutes(1);
});