AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore 13.0.0

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

MediatR.SimpleInjector

Seamless MediatR integration for SimpleInjector with automatic configuration and convention-based registration

CodeFactor Build Status Azure DevOps tests Azure DevOps coverage Quality Gate Status Sonar Coverage Nuget

Why This Library?

If you're using MediatR with SimpleInjector, this library eliminates all the boilerplate. It provides:

  • One-line setup - Scan assemblies and auto-register all handlers, behaviors, and processors
  • Smart defaults - Works out of the box with sensible conventions
  • ASP.NET Core magic - Automatic HttpContext.RequestAborted token propagation to MediatR requests
  • Flexible configuration - Override anything when you need fine-grained control
  • Full MediatR support - Handlers, notifications, streams, behaviors, pre/post processors, and exception handling
  • .NET 10 ready - Updated to the latest .NET and MediatR versions

Quick Start

For ASP.NET Core Projects

Install the package:

dotnet add package AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore

Register MediatR:

using AdaskoTheBeAsT.MediatR.SimpleInjector;

// In your Startup.cs or Program.cs
container.AddMediatRAspNetCore(typeof(Startup));

That's it! All handlers in the assembly containing Startup are registered, and cancellation tokens from HTTP requests are automatically passed to MediatR.

For Other Project Types (Console, WPF, etc.)

Install the package:

dotnet add package AdaskoTheBeAsT.MediatR.SimpleInjector

Register MediatR:

using AdaskoTheBeAsT.MediatR.SimpleInjector;

container.AddMediatR(typeof(MyHandler));

What Gets Registered?

By default, the library registers:

Interface Lifestyle
IMediator Singleton
IRequestHandler<TRequest, TResponse> Transient
INotificationHandler<TNotification> Transient
IStreamRequestHandler<TRequest, TResponse> Transient

Common Usage Patterns

Scanning Multiple Assemblies

// By marker types
container.AddMediatR(typeof(MyHandler), typeof(AnotherHandler));

// By assembly instances
container.AddMediatR(assembly1, assembly2, assembly3);

Scanning All Assemblies in Your Solution

public static class MediatRConfigurator
{
    private const string NamespacePrefix = "YourCompany.YourApp";

    public static void Configure(Container container)
    {
        var assemblies = AppDomain.CurrentDomain.GetAssemblies()
            .Where(a => a.FullName.StartsWith(NamespacePrefix, StringComparison.OrdinalIgnoreCase))
            .ToList();
            
        container.AddMediatR(assemblies.ToArray());
    }
}

ASP.NET Core with Multiple Assemblies

container.AddMediatRAspNetCore(
    typeof(Startup),           // Web project
    typeof(CreateOrderHandler), // Application layer
    typeof(SendEmailHandler)    // Infrastructure layer
);

Configuration Options

Change IMediator Lifestyle

container.AddMediatR(cfg =>
{
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
    cfg.AsScoped(); // Default is Singleton
});

Use Custom IMediator Implementation

container.AddMediatR(cfg =>
{
    cfg.Using<MyCustomMediator>();
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
});

Mock IMediator for Testing

var mockMediator = new Mock<IMediator>();
container.AddMediatR(cfg =>
{
    cfg.Using(() => mockMediator.Object);
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
});

Advanced Configuration

Pipeline Behaviors

Enable all built-in MediatR behaviors:

container.AddMediatR(cfg =>
{
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
    cfg.UsingBuiltinPipelineProcessorBehaviors(true);
});

This registers:

  • RequestPreProcessorBehavior<,> + all IRequestPreProcessor<> implementations
  • RequestPostProcessorBehavior<,> + all IRequestPostProcessor<,> implementations
  • RequestExceptionProcessorBehavior<,> + all IRequestExceptionHandler<,,> implementations
  • RequestExceptionActionProcessorBehavior<,> + all IRequestExceptionAction<,> implementations

Enable specific behaviors only:

container.AddMediatR(cfg =>
{
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
    cfg.UsingBuiltinPipelineProcessorBehaviors(
        requestPreProcessorBehaviorEnabled: true,
        requestPostProcessorBehaviorEnabled: false,
        requestExceptionProcessorBehaviorEnabled: true,
        requestExceptionActionProcessorBehaviorEnabled: false);
});

Add custom pipeline behaviors:

container.AddMediatR(cfg =>
{
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
    cfg.UsingPipelineProcessorBehaviors(
        typeof(LoggingBehavior<,>),
        typeof(ValidationBehavior<,>),
        typeof(TransactionBehavior<,>));
});

Add custom stream pipeline behaviors:

container.AddMediatR(cfg =>
{
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
    cfg.UsingStreamPipelineBehaviors(typeof(StreamLoggingBehavior<,>));
});

Notification Publishers

Use ForeachAwait (default):

container.AddMediatR(cfg =>
{
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
    cfg.WithNotificationPublisherForeachAwait();
});

Use TaskWhenAll for parallel execution:

container.AddMediatR(cfg =>
{
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
    cfg.WithNotificationPublisherTaskWhenAll();
});

Use custom notification publisher:

container.AddMediatR(cfg =>
{
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
    cfg.WithNotificationPublisherCustom<MyCustomPublisher>();
});

Fine-Grained Processor Control

Control exactly which pre/post processors and exception handlers get registered:

container.AddMediatR(cfg =>
{
    cfg.WithHandlerAssemblyMarkerTypes(typeof(MyHandler));
    cfg.UsingBuiltinPipelineProcessorBehaviors(
        requestPreProcessorBehaviorEnabled: true,
        requestPostProcessorBehaviorEnabled: true,
        requestExceptionProcessorBehaviorEnabled: true,
        requestExceptionActionProcessorBehaviorEnabled: true);
    
    // Register specific processors in order
    cfg.WithRequestPreProcessorTypes(
        typeof(LoggingPreProcessor<>),
        typeof(ValidationPreProcessor<>));
    
    cfg.WithRequestPostProcessorTypes(
        typeof(CacheInvalidationPostProcessor<,>),
        typeof(NotificationPostProcessor<,>));
    
    cfg.WithRequestExceptionProcessorTypes(
        typeof(LoggingExceptionProcessor<,,>),
        typeof(RetryExceptionProcessor<,,>));
    
    cfg.WithRequestExceptionActionProcessorTypes(
        typeof(AlertingExceptionAction<,>));
});

Package Information

Package NuGet
AdaskoTheBeAsT.MediatR.SimpleInjector NuGet
AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore NuGet
AdaskoTheBeAsT.MediatR.SimpleInjector.AspNet NuGet

Compatibility

  • .NET Standard 2.0+ - Works with .NET Framework 4.6.1+, .NET Core 2.0+, .NET 5+
  • MediatR 13.1.0+ - Latest MediatR version
  • SimpleInjector 5.5.0+ - Latest SimpleInjector version

Running Tests Locally

dotnet test

The test suite needs a MediatR license key. MediatR resolves it from the environment (MEDIATR_LICENSE_KEY, then the shared LUCKYPENNY_LICENSE_KEY) unless a key is passed explicitly via WithLicenseKey(...). In CI the value comes from the MEDIATR_LICENSE_KEY GitHub secret (see .github/workflows/ci.yml).

To supply it locally, copy the template and fill in your key:

cp .env.example .env
MEDIATR_LICENSE_KEY=your-license-key

.env is git ignored, so the key never lands in the repository. A module initializer in the test projects (test/TestEnvironment.cs, linked into every test project) uses dotenv.net to probe upwards from the test output directory for the nearest .env and exports every KEY=VALUE entry into the test process before the first mediator is resolved. Variables that are already set in the environment are never overwritten, so CI values always win over the file.

This works the same for dotnet test and for the test runners in Visual Studio, Rider, and VS Code. If you prefer not to keep a file in the working tree, set a user-level environment variable instead (setx MEDIATR_LICENSE_KEY "your-license-key" on Windows) and restart your IDE or shell.

Because the test projects also target .NET Framework (net462 - net481), the suite has to be run on Windows.


Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.


License

This project is licensed under the MIT License - see the LICENSE file for details.


Credits

Special thanks to:

This library was inspired by MediatR.Extensions.Microsoft.DependencyInjection and adapted for SimpleInjector's unique capabilities.


<div align="center">

⭐ Star this repo if you find it useful!

Made with ❤️ by Adam "AdaskoTheBeAsT" Pluciński

</div>

Product Compatible and additional computed target framework versions.
.NET 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 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. 
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
13.0.0 42 8/2/2026
12.0.0 621 11/11/2025
11.0.0 259 10/26/2025
10.0.0 5,278 1/5/2025
9.2.0 473 8/18/2024
9.0.3 284 5/27/2024
9.0.2 335 2/18/2024
9.0.1 269 1/27/2024
9.0.0 332 12/2/2023
8.2.0 7,374 7/16/2023
8.1.0 381 5/4/2023
8.0.0 432 2/17/2023
7.1.0 475 1/22/2023
7.0.0 546 11/13/2022
6.0.0 732 10/23/2022
5.1.0 653 7/24/2022
5.0.1 1,402 2/8/2022
5.0.0 728 1/10/2022
4.2.2 883 7/24/2021
4.2.1 579 6/27/2021
Loading failed

- update to .net 10