TailoredApps.Shared.MediatR
2.1.0
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
<PackageReference Include="TailoredApps.Shared.MediatR" Version="2.1.0" />
<PackageVersion Include="TailoredApps.Shared.MediatR" Version="2.1.0" />
<PackageReference Include="TailoredApps.Shared.MediatR" />
paket add TailoredApps.Shared.MediatR --version 2.1.0
#r "nuget: TailoredApps.Shared.MediatR, 2.1.0"
#:package TailoredApps.Shared.MediatR@2.1.0
#addin nuget:?package=TailoredApps.Shared.MediatR&version=2.1.0
#tool nuget:?package=TailoredApps.Shared.MediatR&version=2.1.0
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,0andfalse; a handler that throws caches nothing. Concurrent requests with the same key run the handler once. AnAbsoluteExpirationin 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
GetCacheKeyand add the identity, or one user's response is served to another. HybridCachekeeps entries in process and also uses anIDistributedCachewhen the host registers one.RetryAttemptsis the number of retries after the first failure. WhenExceptionsAllowedBeforeCircuitTriprequests in a row have failed after their retries, requests of that type fail withCircuitBreakerOpenException(RequestType,RetryAfter) for 5 seconds; then one trial request closes or re-opens the circuit. With the default of1, 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.
ValidationBehaviorruns every registeredIValidator<TRequest>synchronously and throws FluentValidation'sValidationExceptionwith all failures.LoggingBehaviorlogs 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
| Product | Versions 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. |
-
net10.0
- FluentValidation (>= 12.2.0)
- MediatR (>= 12.5.0)
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.15)
- Microsoft.Extensions.Caching.Hybrid (>= 10.11.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.15)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.15)
- Microsoft.Extensions.DependencyInjection (>= 10.0.15)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.15)
- Microsoft.Extensions.Options (>= 10.0.15)
- Scrutor (>= 7.0.0)
- TailoredApps.Shared.MediatR.Caching (>= 2.1.0)
-
net11.0
- FluentValidation (>= 12.2.0)
- MediatR (>= 12.5.0)
- Microsoft.Extensions.Caching.Hybrid (>= 11.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 11.0.0)
- Microsoft.Extensions.DependencyInjection (>= 11.0.0)
- Scrutor (>= 7.0.0)
- TailoredApps.Shared.MediatR.Caching (>= 2.1.0)
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 |