alsami / MediatR.Extensions.Autofac.DependencyInjection

Autofac plug-in for MediatR.
MIT License
55 stars 14 forks source link

Generic implementation of RequestPreProcessor of TRequest does not get registered #12

Closed Fandermill closed 1 year ago

Fandermill commented 1 year ago

I'm having trouble getting my IRequestPreProcessor registered with this package.

Let's say I want to log all IRequests that go through the pipeline, no matter what type of IRequest really is. So I create this class:

public class LoggingPreProcessor<TRequest> : IRequestPreProcessor<TRequest> where TRequest : notnull
{
    public Task Process(TRequest request, CancellationToken cancellationToken)
    {
        // logging goes here
        return Task.CompletedTask;
    }
}

I then do this with a ContainerBuilder to register Mediatr:

var configuration = MediatRConfigurationBuilder
    .Create(typeof(IApplicationTag).Assembly) // IApplicationTag is a type in the same assembly as the LoggingPreProcessor type
    .WithAllOpenGenericHandlerTypesRegistered()
    .Build();

builder.RegisterMediatR(configuration);

So I was expecting that .WithAllOpenGenericHandlerTypesRegistered() would pick up my LoggingPreProcessor, but it does not.

When I add this to the registration, it does get registered and called in the pipeline:

builder.RegisterGeneric(typeof(LoggingPreProcessor<>)).As(typeof(IRequestPreProcessor<>));

So I guess that .WithAllOpenGenericHandlerTypesRegistered() only takes closed implementations of Mediatr interfaces, right? Is this on purpose? Or am I missing something?

For open implementations of pipeline behavior, there is .WithCustomPipelineBehaviors(), but that only takes types of IPipelineBehavior<,> and the IRequestPreProcessor<> is not.

alsami commented 1 year ago

Why are you not using IPipelineBehavior<,> instead? I think in general that is the extension point there to use.

Fandermill commented 1 year ago

Thanks for your reply! Yeah, I found this post from mr Bogard from 2016 and the pre processor seems to be intended for request specifics. So yeah, I think you are right. I'll go with the IPipelineBehavior<,>. (https://lostechies.com/jimmybogard/2016/10/13/mediatr-pipeline-examples/)