TailoredApps.Shared.MediatR 2.1.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package TailoredApps.Shared.MediatR --version 2.1.0
                    
NuGet\Install-Package TailoredApps.Shared.MediatR -Version 2.1.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="TailoredApps.Shared.MediatR" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TailoredApps.Shared.MediatR" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="TailoredApps.Shared.MediatR" />
                    
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 TailoredApps.Shared.MediatR --version 2.1.0
                    
#r "nuget: TailoredApps.Shared.MediatR, 2.1.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 TailoredApps.Shared.MediatR@2.1.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=TailoredApps.Shared.MediatR&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=TailoredApps.Shared.MediatR&version=2.1.0
                    
Install as a Cake Tool

TailoredApps.Shared.MediatR

Five MediatR pipeline behaviours registered in one place: logging, FluentValidation, response caching (HybridCache), fallback and retry with a circuit breaker. A request opts in to caching, fallback or retry by having a small policy class next to it; handlers stay free of that code.

Installation

dotnet add package TailoredApps.Shared.MediatR

Registration

using TailoredApps.Shared.MediatR.DI;

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));

var pipeline = new PipelineRegistration(builder.Services);
pipeline.RegisterPipelineBehaviors();                           // the five behaviours and HybridCache
pipeline.RegisterPipelineBehaviors(typeof(Program).Assembly);   // ICachePolicy<,>, IFallbackHandler<,>, IRetryableRequest<,>

// Validators are not scanned by this package; register the ones you have
builder.Services.AddTransient<IValidator<GetProduct>, GetProductValidator>();

Both RegisterPipelineBehaviors calls are needed: the overload that takes an assembly only scans it for policies and fallback handlers, it does not add the behaviours.

Usage

using TailoredApps.Shared.MediatR.Interfaces.Caching;
using TailoredApps.Shared.MediatR.Interfaces.Handlers;
using TailoredApps.Shared.MediatR.Interfaces.Messages;

public record GetProduct(int Id) : IRequest<ProductDto>;

public class GetProductCachePolicy : ICachePolicy<GetProduct, ProductDto>
{
    public TimeSpan? AbsoluteExpirationRelativeToNow => TimeSpan.FromMinutes(30);   // default: 5 minutes
    public IReadOnlyCollection<string>? Tags => ["products"];                       // HybridCache.RemoveByTagAsync("products")
}

public class GetProductRetryPolicy : IRetryableRequest<GetProduct, ProductDto>
{
    public int RetryAttempts => 3;                        // default: 1
    public int RetryDelay => 200;                         // milliseconds, default: 250
    public bool RetryWithExponentialBackoff => true;      // default: false
    public int ExceptionsAllowedBeforeCircuitTrip => 5;   // default: 1
}

public class GetProductFallback : IFallbackHandler<GetProduct, ProductDto>
{
    public Task<ProductDto> HandleFallback(GetProduct request, CancellationToken cancellationToken)
        => Task.FromResult(ProductDto.Unavailable);
}

Good to know

  • Order: Logging → Validation → Caching → Fallback → Retry → handler. A cached response skips fallback, retry and the handler; validation still runs. A response produced by a fallback handler is cached like any other.
  • Every response is cached as returned, including null, 0 and false; a handler that throws caches nothing. Concurrent requests with the same key run the handler once. An AbsoluteExpiration in the past disables caching for that request.
  • The default cache key is the request type name plus a SHA-256 hash of the request serialized as JSON. It does not contain the caller: for per-user or per-tenant data override GetCacheKey and add the identity, or one user's response is served to another.
  • HybridCache keeps entries in process and also uses an IDistributedCache when the host registers one.
  • RetryAttempts is the number of retries after the first failure. When ExceptionsAllowedBeforeCircuitTrip requests in a row have failed after their retries, requests of that type fail with CircuitBreakerOpenException (RequestType, RetryAfter) for 5 seconds; then one trial request closes or re-opens the circuit. With the default of 1, a single failed request opens it. The state is shared by all requests of the type in the process.
  • A request cancelled by its caller is not retried, does not count towards the circuit and is not sent to the fallback.
  • ValidationBehavior runs every registered IValidator<TRequest> synchronously and throws FluentValidation's ValidationException with all failures.
  • LoggingBehavior logs start and finish at Debug and failures at Error, with the request type name and a correlation id. The request object itself is never logged.

Documentation

https://shared.tailoredapps.pl/en/Libraries/MediatR/

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on TailoredApps.Shared.MediatR:

Package Downloads
TailoredApps.Shared.MediatR.ML

Package Description

TailoredApps.Shared.MediatR.ImageClassification

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0-preview.2 45 11/12/2026
3.0.0-preview.1 64 11/12/2026
2.1.0 72 11/12/2026
2.0.1 57 11/12/2026
2.0.0 66 11/12/2026
1.6.0 59 11/12/2026
1.4.19 502 10/26/2026
1.4.18 715 10/23/2026
1.4.17 1,044 10/6/2026
1.4.16 138 9/28/2026
1.4.15 146 9/21/2026
1.4.14 152 8/20/2026
1.4.13 152 8/20/2026
1.4.12 168 8/20/2026
1.4.11 164 8/7/2026
1.4.10 286 7/8/2026
1.4.9 196 7/8/2026
1.4.8 184 7/6/2026
1.4.7 275 5/20/2026
1.4.6 177 5/20/2026
Loading failed