Unable to register IRequestPreProcessors with Mediatr

Seb

I want to register the following dummy IRequestPreProcessor (Mediator 3)

public class IdentifyUserTypeCommandHandler : IRequestPreProcessor<RegisterUserCommand>
{
    private readonly IOptions<TecApiOptions> _options;

    public IdentifyUserTypeCommandHandler(IOptions<TecApiOptions> options)
    {
        _options = options;
    }

    public async Task Process(RegisterUserCommand request)
    {
        request.Type = "internal";
        await Task.FromResult(true);
    }
}

To do so, I have my container setup to map IRequestPreProcessor to my concrete implementation IdentifyUserTypeCommandHandler

        // Pipeline engine used internally to simplify controllers
        services.AddMediatR();
        // Pre-processors
        services.AddTransient(typeof(IRequestPreProcessor<RegisterUserCommand>), typeof(IdentifyUserTypeCommandHandler));

        // Registers command validator
        services.AddTransient(typeof(IValidator<RegisterUserCommand>), typeof(RegisterUserCommandValidator));

        // Registers generic behaviors
        services.AddTransient(typeof(IPipelineBehavior<,>), typeof(Pipeline<,>));
        services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
        services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
        services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));

As soon as I run the code, I get the following exception

System.ArgumentException: Open generic service type 'MediatR.Pipeline.IRequestPreProcessor`1[TRequest]' requires registering an open generic implementation type.

I want to run this pre-processor only for commands of type RegisterUserCommand. Any idea on how I can solve this issue?

FYI for the records,

public class LoggingBehavior<TCommand, TResponse> : IPipelineBehavior<TCommand, TResponse>
{
    private readonly ILogger _logger;

    public LoggingBehavior(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory?.CreateLogger(typeof(TCommand).Name) ?? throw new ArgumentNullException(nameof(loggerFactory));
    }

    public async Task<TResponse> Handle(TCommand request, RequestHandlerDelegate<TResponse> next)
    {
        try
        {
            _logger.LogInformation(LoggingEvents.RUN_HANDLER, $"Handling '{typeof(TCommand).Name}'");
            var response = await next();
            _logger.LogInformation(LoggingEvents.RUN_HANDLER, $"Handled '{typeof(TResponse).Name}'");
            return response;
        }
        catch (Exception e)
        {
            _logger.LogError(
                LoggingEvents.RUN_HANDLER_EXCEPTION, e,
                $"An error occured while processing pipeline '{GetType().Name}' [{typeof(TCommand).Name} >> {typeof(TResponse).Name}]");
            throw;
        }
    }
}

Thank you, Regards, Seb

Seb

So after talking to the lib's creator, it seems that Pre & Post processors must have a generic definition.

public class GenericRequestPostProcessor<TRequest, TResponse> : IRequestPostProcessor<TRequest, TResponse>

OR

public class GenericRequestPreProcessor<TRequest> : IRequestPreProcessor<TRequest>

src: Examples

The <TRequest> and <TRequest, TResponse> are required when creating a new Pre/Post processors. Now the 1 000 000$ dollars question I will try to answer later:

How do we specialize processors so they deal with a specific set of requests/commands (without having to check request type)...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Autofac register interface MediatR

From Dev

Unable to register a blueprint

From Dev

Unable to register NSD Service

From Dev

Unable to redirect to register page

From Dev

Unable to register a blueprint

From Dev

Unable to register kprobe

From Dev

MvvmCross MvxApplication unable to register types

From Dev

Unable to register Bower package: EINVFORMAT

From Dev

MvvmCross MvxApplication unable to register types

From Dev

Unable to register SIP via WiFi

From Dev

Unable to register Docker service with Registrator

From Dev

Unable to update a column in a table on register

From Dev

unable to register node to hub in selenium

From Dev

Unable to register endpoint startup with ServiceControl

From Dev

Unable to update shift register with NodeMCU

From Dev

Unable to access register variable in tasks

From Dev

Unable to register multiple instance of eureka

From Dev

Unable to register my application with Google Developers

From Dev

Unable to register DbConnection with Unity and Entity Framework

From Dev

Unable to register an user in Ejabberd using XMPPFramework

From Dev

Unable to register and add automapper profile in Autofac

From Dev

Unable to register the PushPlugin in app using Phonegap Build?

From Dev

UWP Unable to register app for Windows Store release

From Dev

Unable to register Google Play - Developer console

From Dev

Unable to register services to the discovery server (EUREKA)

From Dev

Unable to register git runner on local network server

From Dev

Unable to register my application with Google Developers

From Dev

Unable to register the PushPlugin in app using Phonegap Build?

From Dev

unable to login wordpress user which i register

Related Related

HotTag

Archive