Scans assemblies and adds handlers, preprocessors, and postprocessors implementations to the container. To use, with an IServiceCollection
instance:
services.AddMediatR(typeof(MyHandler));
or with an assembly:
services.AddMediatR(typeof(Startup).GetTypeInfo().Assembly);
This registers:
IMediator
as transientIRequestHandler<>
concrete implementations as transientINotificationHandler<>
concrete implementations as transientIRequestPreProcessor<>
concrete implementations as transientIRequestPostProcessor<,>
concrete implementations as transientIRequestExceptionHandler<,,>
concrete implementations as transientIRequestExceptionAction<,>)
concrete implementations as transient
This also registers open generic implementations for:
INotificationHandler<>
IRequestPreProcessor<>
IRequestPostProcessor<,>
IRequestExceptionHandler<,,>
IRequestExceptionAction<,>
Keep in mind that the built-in container does not support constrained open generics. If you want this behavior, you will need to add any one of the conforming containers.
To customize registration, such as lifecycle or the registration type:
services.AddMediatR(cfg => cfg.Using<MyCustomMediator>().AsSingleton(), typeof(Startup));
To register behaviors, register them individually before or after calling AddMediatR
.
If you have an open generic not listed above, you'll need to register it explicitly. For example, if you have an open generic request handler, register the open generic types explicitly:
services.AddTransient(typeof(IRequestHandler<,>), typeof(GenericHandlerBase<,>));
This won't work with generic constraints, so you're better off creating an abstract base class and concrete closed generic classes that fill in the right types.