I have a slightly modified version of Mediatr that handles command processing in my application. I implemented MediatorPipeline, which allows me to have before and after processors.
public class AsyncMediatorPipeline<TRequest, TResponse>
: IAsyncRequestHandler<TRequest, TResponse>
where TRequest : IAsyncRequest<TResponse>
{
private readonly IAsyncRequestHandler<TRequest, TResponse> inner;
private readonly IAsyncPreRequestHandler<TRequest>[] preRequestHandlers;
private readonly IAsyncPostRequestHandler<TRequest,TResponse>[] postRequestHandlers;
public AsyncMediatorPipeline(IAsyncRequestHandler<TRequest, TResponse> inner,
IAsyncPreRequestHandler<TRequest>[] preRequestHandlers,
IAsyncPostRequestHandler<TRequest, TResponse>[] postRequestHandlers)
{
this.inner = inner;
this.preRequestHandlers = preRequestHandlers;
this.postRequestHandlers = postRequestHandlers;
}
public async Task<TResponse> Handle(TRequest message)
{
foreach (var preRequestHandler in preRequestHandlers)
{
await preRequestHandler.Handle(message);
}
var result = await inner.Handle(message);
foreach (var postRequestHandler in postRequestHandlers)
{
await postRequestHandler.Handle(message, result);
}
return result;
}
}
I register my preprocessors with autofac, for example:
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.As(type => type.GetInterfaces()
.Where(type => type.IsClosedTypeOf(typeof(IAsyncPreRequestHandler<>))))
.InstancePerLifetimeScope();
At runtime, I get duplicate pre-processors. I have to filter out the duplicate set. I do not know why this is happening. If I comment on the registration, I do not receive any preprocessors, which indicates that I am not duplicating the registration elsewhere.
Update. Below is some information about the type that appears to be registered twice. Different class definitions:
Concrete handler
public class ClientEditorFormIdentifierValidationHandler
: IAsyncPreRequestHandler<AddOrEditClientCommand>{}
Team class
public class AddOrEditClientCommand
: IAsyncRequest<ICommandResult>{}
Command interface
public interface IAsyncRequest<out TResponse> { }