PollyMediatR 1.0.0

dotnet add package PollyMediatR --version 1.0.0
                    
NuGet\Install-Package PollyMediatR -Version 1.0.0
                    
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="PollyMediatR" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PollyMediatR" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="PollyMediatR" />
                    
Project file
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 PollyMediatR --version 1.0.0
                    
#r "nuget: PollyMediatR, 1.0.0"
                    
#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 PollyMediatR@1.0.0
                    
#: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=PollyMediatR&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=PollyMediatR&version=1.0.0
                    
Install as a Cake Tool

PollyMediatR

NuGet NuGet Downloads CI

Polly v8 resilience pipelines for MediatR — add retry, timeout, circuit-breaker, rate-limiting, hedging and chaos engineering to any MediatR request handler with a single line of registration. No changes to handler code required.

services.AddPollyMediatR(pipeline =>
    pipeline
        .AddRetry(new RetryStrategyOptions
        {
            MaxRetryAttempts = 3,
            Delay = TimeSpan.FromMilliseconds(200),
            ShouldHandle = new PredicateBuilder().Handle<Exception>(),
        })
        .AddTimeout(TimeSpan.FromSeconds(5)));

Every IRequest<T> handler is now automatically wrapped with retry + timeout — zero changes to existing handlers.


Why PollyMediatR?

MediatR's IPipelineBehavior<TRequest, TResponse> is the natural place to apply cross-cutting resilience concerns, but wiring it up with Polly v8 requires boilerplate. PollyMediatR does the wiring for you.

Without PollyMediatR With PollyMediatR
Write a custom IPipelineBehavior per pipeline One AddPollyMediatR(...) call
Manually inject ResiliencePipeline into each behavior Pipeline registered & injected automatically
Duplicate retry/timeout logic across query & command handlers Single pipeline applied to all handlers
Must update handlers to apply new resilience policies Zero changes to existing handlers

Installation

dotnet add package PollyMediatR

Targets net6.0, net8.0, and net9.0.

Dependencies: Polly.Core 8.*, MediatR 12.*, Microsoft.Extensions.DependencyInjection.Abstractions 8.*


Quick start

using Polly.Retry;
using PollyMediatR;

services.AddPollyMediatR(pipeline =>
    pipeline.AddRetry(new RetryStrategyOptions
    {
        MaxRetryAttempts = 3,
        Delay = TimeSpan.FromMilliseconds(200),
        BackoffType = DelayBackoffType.Exponential,
        ShouldHandle = new PredicateBuilder().Handle<Exception>(),
    }));

2. Register with a pre-built pipeline

var pipeline = new ResiliencePipelineBuilder()
    .AddRetry(new RetryStrategyOptions { ... })
    .AddTimeout(TimeSpan.FromSeconds(10))
    .Build();

services.AddPollyMediatR(pipeline);

3. Use normally with MediatR — nothing changes

// Handler — no Polly code needed
public class GetOrderHandler : IRequestHandler<GetOrderQuery, Order>
{
    public async Task<Order> Handle(GetOrderQuery request, CancellationToken ct)
    {
        return await _db.Orders.FindAsync(request.Id, ct); // retried automatically
    }
}

// At the call site — identical to normal MediatR usage
var order = await mediator.Send(new GetOrderQuery(id));

ASP.NET Core example

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMediatR(cfg =>
    cfg.RegisterServicesFromAssemblyContaining<Program>());

builder.Services.AddPollyMediatR(pipeline =>
    pipeline
        .AddRetry(new RetryStrategyOptions
        {
            MaxRetryAttempts = 3,
            Delay = TimeSpan.FromMilliseconds(100),
            BackoffType = DelayBackoffType.Exponential,
            ShouldHandle = new PredicateBuilder()
                .Handle<HttpRequestException>()
                .Handle<TimeoutException>(),
        })
        .AddTimeout(TimeSpan.FromSeconds(30))
        .AddCircuitBreaker(new CircuitBreakerStrategyOptions
        {
            FailureRatio = 0.5,
            MinimumThroughput = 10,
            SamplingDuration = TimeSpan.FromSeconds(30),
            BreakDuration = TimeSpan.FromSeconds(15),
        }));

Pipeline order

Polly strategies are applied outer-to-inner (left-to-right). The recommended order is:

[Timeout] → [Retry] → [Circuit Breaker] → [Handler]
pipeline
    .AddTimeout(TimeSpan.FromSeconds(10))    // 1. Overall deadline
    .AddRetry(retryOptions)                  // 2. Retry on failure
    .AddCircuitBreaker(cbOptions)            // 3. Open circuit if overloaded

Combining with chaos engineering (PollyChaos)

Use PollyChaos to harden your handlers in test/staging:

services.AddPollyMediatR(pipeline =>
    pipeline
        .AddRetry(retryOptions)
        .AddChaosFault(injectionRate: 0.1)); // inject faults 10% of the time

Package Downloads Description
PollyBackoff Downloads Jitter, linear & custom backoff for Polly v8 retry
PollyChaos Downloads Fault & latency injection (Simmy for Polly v8)
PollyCaching Downloads Cache-aside resilience strategy for Polly v8
PollyBulkhead Downloads Bulkhead / concurrency limiter for Polly v8
PollyOpenTelemetry Downloads OpenTelemetry metrics & tracing for Polly v8

License

MIT

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 46 6/23/2026

1.0.0: Initial release. ResiliencePipelineBehavior<TRequest,TResponse> wraps any MediatR request in a Polly v8 ResiliencePipeline. Supports net6.0, net8.0, and net9.0.