AdaskoTheBeAsT.MediatR.SimpleInjector 9.2.0

dotnet add package AdaskoTheBeAsT.MediatR.SimpleInjector --version 9.2.0                
NuGet\Install-Package AdaskoTheBeAsT.MediatR.SimpleInjector -Version 9.2.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" Version="9.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AdaskoTheBeAsT.MediatR.SimpleInjector --version 9.2.0                
#r "nuget: AdaskoTheBeAsT.MediatR.SimpleInjector, 9.2.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.
// Install AdaskoTheBeAsT.MediatR.SimpleInjector as a Cake Addin
#addin nuget:?package=AdaskoTheBeAsT.MediatR.SimpleInjector&version=9.2.0

// Install AdaskoTheBeAsT.MediatR.SimpleInjector as a Cake Tool
#tool nuget:?package=AdaskoTheBeAsT.MediatR.SimpleInjector&version=9.2.0                

AdaskoTheBeAsT.MediatR.SimpleInjector and AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore

MediatR extensions for SimpleInjector.

Badges

CodeFactor Build Status Azure DevOps tests Azure DevOps coverage Quality Gate Status Sonar Tests Sonar Test Count Sonar Test Execution Time Sonar Coverage Nuget

Usage in AspNetCore

Scans assemblies and adds handlers, preprocessors, and postprocessors implementations to the SimpleInjector container. Additionally it register decorator which passes HttpContext.RequestAborted cancellation token from asp.net core controllers to MediatR.
Install package AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore.
There are few options to use with Container instance:

  1. Marker type from assembly which will be scanned

    container.AddMediatRAspNetCore(typeof(MyHandler), type2 /*, ...*/);
    
  2. Assembly which will be scanned

    container.AddMediatRAspNetCore(assembly, assembly2 /*, ...*/);
    
  3. Full configuration

    var testMediator = new Mock<IMediator>();
    container.AddMediatR(
        cfg =>
        {
            cfg.Using(() => testMediator.Object);
            cfg.WithHandlerAssemblyMarkerTypes(typeof(MyMarkerType));
            cfg.UsingBuiltinPipelineProcessorBehaviors(true);
            cfg.UsingPipelineProcessorBehaviors(typeof(CustomPipelineBehavior<,>));
            cfg.UsingStreamPipelineBehaviors(typeof(CustomStreamPipelineBehavior<,>));
        });
    

Usage in other project types

Scans assemblies and adds handlers, preprocessors, and postprocessors implementations to the SimpleInjector container.
Install package AdaskoTheBeAsT.MediatR.SimpleInjector.
There are few options to use with Container instance:

  1. Marker type from assembly which will be scanned

    container.AddMediatR(typeof(MyHandler), type2 /*, ...*/);
    
  2. List of assemblies which will be scanned.

    Below is sample for scanning assemblies from some solution.

    [ExcludeFromCodeCoverage]
    public static class MediatRConfigurator
    {
        private const string NamespacePrefix = "YourNamespace";
    
        public static void Configure(Container container)
        {
            var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
            var assemblies = new List<Assembly>();
            var mainAssembly = typeof(MediatRConfigurator).GetTypeInfo().Assembly;
            var refAssemblies = mainAssembly.GetReferencedAssemblies();
            foreach (var assemblyName in refAssemblies
                .Where(a => a.FullName.StartsWith(NamespacePrefix, StringComparison.OrdinalIgnoreCase)))
                {
                    var assembly = loadedAssemblies.Find(l => l.FullName == assemblyName.FullName)
                        ?? AppDomain.CurrentDomain.Load(assemblyName);
                    assemblies.Add(assembly);
                }
            container.AddMediatR(assemblies);
        }
    }
    

This will register:

  • IMediator as Singleton
  • IRequestHandler<> concrete implementations as Transient
  • INotificationHandler<> concrete implementations as Transient
  • IStreamRequestHandler<> concrete implementations as Transient

Advanced usage

Setting up custom IMediator instance and marker type from assembly for unit testing (Moq sample)

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

Setting up custom IMediator implementation and marker type from assembly

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

Setting up custom IMediator implementation and assemblies to scan

 container.AddMediatR(
     cfg =>
     {
         cfg.Using<MyCustomMediator>();
         cfg.WithAssembliesToScan(assemblies);
     });

Setting assemblies to scan and different lifetime for IMediator implementation

 container.AddMediatR(
     cfg =>
     {
         cfg.WithAssembliesToScan(assemblies);
         cfg.AsScoped();
     });

Setting assemblies to scan and additionally enabling all builtin behaviors and user defined processors/handlers

This will register following behaviors:

  • RequestPreProcessorBehavior<,>
  • RequestPostProcessorBehavior<,>
  • RequestExceptionProcessorBehavior<,>
  • RequestExceptionActionProcessorBehavior<,>

and all user defined implementation of processors and handlers:

  • IRequestPreProcessor<>

  • IRequestPostProcessor<,>

  • IRequestExceptionHandler<,,>

  • IRequestExceptionActionHandler<,>

      container.AddMediatR(
          cfg =>
          {
              cfg.WithAssembliesToScan(assemblies);
              cfg.UsingBuiltinPipelineProcessorBehaviors(true);
          });
    

Setting assemblies to scan and additionally enabling chosen builtin behaviors and user defined processors/handlers

This will register following behaviors:

  • RequestPreProcessorBehavior<,>
  • RequestExceptionProcessorBehavior<,>

and all user defined implementation of processors and handlers:

  • IRequestPreProcessor<>

  • IRequestExceptionHandler<,,>

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

Setting assemblies to scan and additionally custom stream request handlers behaviors

This will register following stream behaviors:

  • CustomStreamPipelineBehavior<,>

      container.AddMediatR(
          cfg =>
          {
              cfg.WithAssembliesToScan(assemblies);
              cfg.UsingStreamPipelineBehaviors(typeof(CustomStreamPipelineBehavior<,>));
          });
    

Setting assemblies to scan and additionally enabling chosen builtin behaviors and user defined processors/handlers also with custom Pipeline Process Behaviors

 container.AddMediatR(
     cfg =>
     {
         cfg.WithAssembliesToScan(assemblies);
         cfg.UsingBuiltinPipelineProcessorBehaviors(
             requestPreProcessorBehaviorEnabled: true,
             requestPostProcessorBehaviorEnabled: false,
             requestExceptionProcessorBehaviorEnabled: true,
             requestExceptionActionProcessorBehaviorEnabled: false);
         cfg.UsingPipelineProcessorBehaviors(typeof(CustomPipelineBehavior<,>));
         cfg.WithRequestPreProcessorTypes(typeof(FirstRequestPreProcessor<>), typeof(SecondRequestPreProcessor<>));
         cfg.WithRequestPostProcessorTypes(typeof(FirstRequestPostProcessor<,>), typeof(SecondRequestPostProcessor<,>));
         cfg.WithRequestExceptionProcessorTypes(typeof(FirstRequestExceptionProcessor<,,>), typeof(SecondRequestExceptionProcessor<,,>));
         cfg.WithRequestExceptionActionProcessorTypes(typeof(FirstRequestExceptionActionProcessor<,>), typeof(SecondRequestExceptionActionProcessor<,>));
     });

Setting assemblies to scan and additionally enabling chosen builtin behaviors and processors with specific order

Setting assemblies to scan and set WithNotificationPublisherForeachAwait NotificationPublisher

 container.AddMediatR(
     cfg =>
     {
         cfg.WithAssembliesToScan(assemblies);
         cfg.WithNotificationPublisherForeachAwait();
     });
``

### Setting assemblies to scan and set TaskWhenAllPublisher NotificationPublisher

```cs
 container.AddMediatR(
     cfg =>
     {
         cfg.WithAssembliesToScan(assemblies);
         cfg.WithNotificationPublisherTaskWhenAll();
     });

Setting assemblies to scan and set custom NotificationPublisher

 container.AddMediatR(
     cfg =>
     {
         cfg.WithAssembliesToScan(assemblies);
         cfg.WithNotificationPublisherCustom<CustomNotificationPublisher>();
     });

Thanks to

Code originates from MediatR.Extensions.Microsoft.DependencyInjection and was changed to work with SimpleInjector.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
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 AdaskoTheBeAsT.MediatR.SimpleInjector:

Package Downloads
AdaskoTheBeAsT.MediatR.SimpleInjector.AspNetCore

Allows easy configuration of MediatR and its registration in SimpleInjector in connection to ASP NET Core and ASP NET 5.

AdaskoTheBeAsT.MediatR.SimpleInjector.AspNet

Allows easy configuration of MediatR and its registration in SimpleInjector in connection to ASP NET Full Framework (4.6.1 until 4.8).

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.2.0 563 8/18/2024
9.0.3 339 5/27/2024
9.0.2 1,718 2/18/2024
9.0.1 191 1/27/2024
9.0.0 271 12/2/2023
8.2.0 8,830 7/16/2023
8.1.0 282 5/4/2023
8.0.0 373 2/17/2023
7.1.0 454 1/22/2023
7.0.0 692 11/13/2022
6.0.0 653 10/23/2022
5.1.0 688 7/24/2022
5.0.1 1,236 2/8/2022
5.0.0 703 1/10/2022
4.2.2 4,226 7/24/2021
4.2.1 500 6/27/2021
4.2.0 553 3/7/2021
4.1.1 582 1/16/2021
4.1.0 616 12/16/2020
4.0.1 651 11/29/2020
4.0.0 575 11/11/2020
3.1.0 752 10/25/2020
3.0.1 725 10/16/2020
3.0.0 648 10/10/2020
2.2.0 644 10/7/2020
2.1.0 619 8/1/2020
2.0.6 693 7/25/2020
2.0.5 641 7/14/2020
2.0.4 505 7/14/2020
2.0.3 629 7/2/2020
2.0.2 639 6/15/2020
2.0.1 574 6/15/2020 2.0.1 is deprecated because it is no longer maintained.
2.0.0 695 6/14/2020
1.4.3 577 5/20/2020
1.4.2 765 4/21/2020
1.4.1 715 3/7/2020
1.4.0 701 3/1/2020
1.3.2 638 2/27/2020
1.3.1 574 2/27/2020 1.3.1 is deprecated.
1.3.0 753 2/23/2020
1.2.0 645 2/22/2020
1.1.0 800 1/9/2020
1.0.0 659 1/5/2020

- added ability to order RequestPreProcessors, RequestPostProcessors, RequestExceptionHandlers, RequestExceptionActions