我试图了解如何使用中介程序设置管道行为。
我的命令:
public class MyUpdateCommand : IRequest<CommandResult>
{
// fields
}
我的处理程序:
public class MyUpdateCommandHandler : RequestHandler<MyUpdateCommand, CommandResult>
{
private readonly IMyRepository _repository;
public GasDetailsUpdateCommandHandler(IMyRepository repository)
{
_repository = repository;
}
protected override CommandResult HandleCore(MyUpdateCommand command)
{
{
_repository.Update(command);
return CommandResult.Success;
}
}
}
现在,假设我要处理在RequestHandlers中引发的所有异常,这些异常通过记录它们并在我的CommandResult
对象中返回消息来返回CommandResult
。我创建了此类:
public class ExceptionLoggingBehavior<TRequest, TResponse>
: IPipelineBehavior<TRequest, TResponse> where TResponse : CommandResult
{
private readonly ILog _logger;
public ExceptionLoggingBehavior(ILog logger)
{
_logger = logger;
}
public async Task<TResponse> Handle(TRequest request,
CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
try
{
return await next();
}
catch (Exception ex)
{
_logger.Error(ex.Message);
return (TResponse) CommandResult.Fail(ex.Message);
}
}
}
我需要在控制容器反转(AutoFac)中添加什么?