Nie mogę zmusić MediatR i SimpleInjectora do współpracy w mojej solucji.
Wygląda to tak:
- Projekt Web - tutaj jest konfiguracja SimpleInjectora i tu znajdują się CommandHandlery
- Projekt Infrastruktura - tu znajdują się QueryHandlery
- Projekt Domain - tu znajdują się czyste komendy i query
Konfiguracja SimpleInjectora wygląda tak:
Zaczerpnięta z https://github.com/jbogard/MediatR/blob/master/samples/MediatR.Examples.SimpleInjector/Program.cs
void IModule.Load(Container container)
{
var assemblies = GetAssemblies().ToArray();
container.RegisterSingleton<IMediator, Mediator>();
container.Register(typeof(IRequestHandler<,>), assemblies);
container.Register(typeof(IAsyncRequestHandler<,>), assemblies);
container.Register(typeof(IRequestHandler<>), assemblies);
container.Register(typeof(IAsyncRequestHandler<>), assemblies); //
container.Register(typeof(ICancellableAsyncRequestHandler<>), assemblies);
container.RegisterCollection(typeof(INotificationHandler<>), assemblies);
container.RegisterCollection(typeof(IAsyncNotificationHandler<>), assemblies);
container.RegisterCollection(typeof(ICancellableAsyncNotificationHandler<>), assemblies);
container.RegisterCollection(typeof(IRequestPreProcessor<>), assemblies);
//Pipeline
container.RegisterCollection(typeof(IPipelineBehavior<,>), new[]
{
typeof(RequestPreProcessorBehavior<,>),
typeof(RequestPostProcessorBehavior<,>)
});
container.RegisterSingleton(new SingleInstanceFactory(container.GetInstance));
container.RegisterSingleton(new MultiInstanceFactory(container.GetAllInstances));
}
private static IEnumerable<Assembly> GetAssemblies()
{
yield return typeof(IMediator).GetTypeInfo().Assembly;
yield return typeof(CashCaseCreateCommandHandler).GetTypeInfo().Assembly;
yield return typeof(CashCaseGetAllQueryHandler).GetTypeInfo().Assembly;
}
Tylko, że musiałem wyciąć:
//Pipeline
container.RegisterCollection(typeof(IPipelineBehavior<,>), new []
{
typeof(RequestPreProcessorBehavior<,>),
typeof(RequestPostProcessorBehavior<,>),
typeof(GenericPipelineBehavior<,>)
});
container.RegisterCollection(typeof(IRequestPreProcessor<>), new [] { typeof(GenericRequestPreProcessor<>)});
container.RegisterCollection(typeof(IRequestPostProcessor<,>), new[] { typeof(GenericRequestPostProcessor<,>) });
Bo nie widzi czegoś takiego jak:
typeof(GenericPipelineBehavior<,>)
container.RegisterCollection(typeof(IRequestPreProcessor<>), new [] { typeof(GenericRequestPreProcessor<>)});
container.RegisterCollection(typeof(IRequestPostProcessor<,>), new[] { typeof(GenericRequestPostProcessor<,>) });
Błąd jaki się pojawia w czasie uruchomienia komendy i wysłania do MediatR to:
"The constructor of type RequestPostProcessorBehavior<CashCaseCreateCommand, Guid> contains the parameter with name 'postProcessors' and type IEnumerable<IRequestPostProcessor<CashCaseCreateCommand, Guid>> that is not registered. Please ensure IEnumerable<IRequestPostProcessor<CashCaseCreateCommand, Guid>> is registered, or change the constructor of RequestPostProcessorBehavior<CashCaseCreateCommand, Guid>."
Jak to poprawić?
Można zrobić jakieś skanowanie solucji by nie wpisywać ręcznie nowo dodanych handlerów?