无法向Mediatr注册IRequestPreProcessors

时间:2017-07-18 06:28:40

标签: dependency-injection asp.net-core mediatr

我想注册以下虚拟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);
    }
}

为此,我将容器设置为将IRequestPreProcessor映射到我的具体实现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<,>));

一旦我运行代码,我就会得到以下异常

  

System.ArgumentException:打开通用服务类型'MediatR.Pipeline.IRequestPreProcessor`1 [TRequest]'需要注册一个开放的通用实现类型。

我想仅为RegisterUserCommand类型的命令运行此预处理器。关于如何解决这个问题的任何想法?

FYI的记录,

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;
        }
    }
}

谢谢你, 问候, SEB

2 个答案:

答案 0 :(得分:1)

所以在与lib的创建者交谈之后,似乎Pre&amp;后处理器必须具有通用定义。

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

OR

public class GenericRequestPreProcessor<TRequest> : IRequestPreProcessor<TRequest>

src:Examples

创建新的Pre / Post处理器时需要<TRequest><TRequest, TResponse>。现在我将尝试回答1 000 000美元的问题:

  

我们如何专门处理处理器,以便处理一组特定的请求/命令(无需检查请求类型)......

答案 1 :(得分:0)

如果您正在使用services.AddMediatR()

确保注册内置行为:RequestPreProcessorBehavior

services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
services.AddMediatR();

然后您的预处理器需要按照@ Sebastien的回答进行通用定义:

public class PingDecorator<TRequest> : IRequestPreProcessor<TRequest>
{
    public Task Process(TRequest request)
    {
        return Unit.Task;
    }
}

如果您希望预处理器仅用于特定请求:

public class PingDecorator<TRequest> : IRequestPreProcessor<TRequest> where TRequest:Ping
{
    public Task Process(TRequest request)
    {
        request.Message = request.Message + " Decorated";
        return Unit.Task;
    }
}