註冊IRequestPreProcessors我要註冊以下虛擬IRequestPreProcessor(中保3)無法與Mediatr
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<,>));
只要我運行代碼,我會得到以下異常
Sy stem.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;
}
}
}
謝謝 問候, 勒布
我假設'LoggingBehavior'和'ValidationBehavior '是裝飾者。您嘗試註冊的方式('IPipelineBehavior <,>')裝飾器,但通過堆疊它們將無法與.NET Core容器一起使用。 .NET Core不支持裝飾器。如果存在非通用註冊,則該註冊將首先進行,否則將選擇最後的註冊,但根據其自身而定,它將循環。 –
Steven
@Steven我已經檢查過,看來我的行爲按預期工作(至少當我調試我的應用程序時)。我主要是遵循這裏提到的東西>> https://github.com/jbogard/MediatR/wiki/Behaviors – Seb
啊,我知道,管道行爲是_not_裝飾者。那麼我完全不同意這個:「這是一種更自然的方式來提高你的處理程序的行爲」,這是真的「在容器中更好地支持」,但這是國際海事組織對不做'正確的事情'的一個壞主張。 – Steven