MediatR extensions for SimpleInjector.
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:
-
Marker type from assembly which will be scanned
container.AddMediatRAspNetCore(typeof(MyHandler), type2 /*, ...*/);
-
Assembly which will be scanned
container.AddMediatRAspNetCore(assembly, assembly2 /*, ...*/);
-
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<,>)); });
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:
-
Marker type from assembly which will be scanned
container.AddMediatR(typeof(MyHandler), type2 /*, ...*/);
-
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 SingletonIRequestHandler<>
concrete implementations as TransientINotificationHandler<>
concrete implementations as TransientIStreamRequestHandler<>
concrete implementations as Transient
var testMediator = new Mock<IMediator>();
container.AddMediatR(
cfg =>
{
cfg.Using(() => testMediator.Object);
cfg.WithHandlerAssemblyMarkerTypes(typeof(MyMarkerType));
});
container.AddMediatR(
cfg =>
{
cfg.Using<MyCustomMediator>();
cfg.WithHandlerAssemblyMarkerTypes(typeof(MyMarkerType));
});
container.AddMediatR(
cfg =>
{
cfg.Using<MyCustomMediator>();
cfg.WithAssembliesToScan(assemblies);
});
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); });
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
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();
});
container.AddMediatR(
cfg =>
{
cfg.WithAssembliesToScan(assemblies);
cfg.WithNotificationPublisherCustom<CustomNotificationPublisher>();
});
- Jimmy Boggard for MediatR
- Steven van Deursen for SimpleInjector
- Sebastian Kleinschmager for idea of automatic passing RequestAborted to MediatR
- Konrad Rudolph for idea of IsAssignableToGenericType
Code originates from MediatR.Extensions.Microsoft.DependencyInjection and was changed to work with SimpleInjector.