我已经在Mediatr
项目中实现了.NET framework
,并且想使用IPipelineBehavior
。
我已经使用项目中的示例实现并注册了容器:https://github.com/jbogard/MediatR/blob/master/samples/MediatR.Examples.Unity/Program.cs
这是我的行为
public class AuditPipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly ILeaveAuditTrail _auditor;
public AuditPipelineBehavior(ILeaveAuditTrail auditor)
{
_auditor = auditor;
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
if (AuditPrevented(request))
return await next();
var response = await next();
var auditLog = _auditor.CreateAuditLog(request, response);
//Dispatch audit
return response;
}
private static bool AuditPrevented<TInput>(TInput query)
{
return query.GetType().GetCustomAttributes(typeof(PreventAuditAttribute), true).Any();
}
}
我和Mediatr一起注册就是这样>>
container.RegisterMediator(new HierarchicalLifetimeManager()); container.RegisterMediatorHandlers(applicationAssembly); container.RegisterType(typeof(IPipelineBehavior<,>), typeof(AuditPipelineBehavior<,>)); container.RegisterType<ILeaveAuditTrail, DefaultAuditor>();
当我发送带有Mediatr的IRequest时,它可以很好地处理并且得到结果,但是没有调用AuditPipeline。
如果我删除async
关键字并仅返回next();
,则可以。但是这样一来,我无法正确拦截我的回应。
同样在示例Github上,句柄也是异步实现的:https://github.com/jbogard/MediatR/wiki/Behaviors
我已经在.NET Framework项目中实现了Mediatr,并希望使用IPipelineBehavior。我已经使用该项目中的示例实现并注册了该容器:https:// github ....
该问题与Mediatr
或PipelineBehavior
的注册无关。