Adds support to use MediatR with SimpleInjector's ScopedLifestyle.Flowing.
- MediatRSimpleInjectorLab - Minimal getting started project.
You need register everything by yourself. There is no helper class for that, because some configuration is up to the user. Example:
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var container = new Container();
container.Options.DefaultScopedLifestyle = ScopedLifestyle.Flowing;
container.Register<IMediator, MediatRScoped>(Lifestyle.Scoped);
container.Register<ISender, MediatRScoped>(Lifestyle.Scoped);
container.Register<IPublisher, MediatRScoped>(Lifestyle.Scoped);
container.Register(typeof(IRequestHandler<,>), assemblies);
container.RegisterHandlers(typeof(INotificationHandler<>), assemblies);
container.RegisterHandlers(typeof(IRequestExceptionAction<,>), assemblies);
container.RegisterHandlers(typeof(IRequestExceptionHandler<,,>), assemblies);
container.RegisterHandlers(typeof(IStreamRequestHandler<,>), assemblies);
//Pipeline
container.Collection.Register(typeof(IPipelineBehavior<,>), new[]
{
typeof(RequestExceptionProcessorBehaviorScoped<,>),
typeof(RequestExceptionActionProcessorBehaviorScoped<,>),
typeof(RequestPreProcessorBehavior<,>),
typeof(RequestPostProcessorBehavior<,>),
}, Lifestyle.Scoped);
container.Collection.Register(typeof(IRequestPreProcessor<>), new[] { typeof(EmptyRequestPreProcessor<>) });
container.Collection.Register(typeof(IRequestPostProcessor<,>), new[] { typeof(EmptyRequestPostProcessor<,>) });
using Scope scope = new Scope(container);
// You will have to resolve directly from the scope.
var instance = scope.GetInstance<ScopedInstance>();
For more details refer to SimpleInjector documentation