我想尝试在MediatR新管道特点:https://github.com/jbogard/MediatR/wiki/Behaviors如何注册一个MediatR后处理器
我尝试以下,但它并没有得到执行
services.AddMediatR();
services.AddTransient(typeof(IRequestPostProcessor<,>), typeof(PostHandler<,>));
我在想什么?
我想尝试在MediatR新管道特点:https://github.com/jbogard/MediatR/wiki/Behaviors如何注册一个MediatR后处理器
我尝试以下,但它并没有得到执行
services.AddMediatR();
services.AddTransient(typeof(IRequestPostProcessor<,>), typeof(PostHandler<,>));
我在想什么?
您需要注册与后处理器相关的行为,如this unit test显示。
您的注册代码是这样:
services.AddMediatR();
services.AddTransient(typeof(IRequestPostProcessor<,>), typeof(PostHandler<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
这behavior将得到所有你注册的后处理器并执行它们。
编辑
关于后处理器运行两次评论之后,我看了一下,在ASP.NET内核寄存器MediatR内置DI容器中的代码,和原来的实例IRequestPreProcessor<TRequest, TResponse>
和IRequestPostProcessor<TRequest, TResponse>
会自动注册,您可以看到here。如何让他们在管道中运行只需注册相关的行为。那么必要的注册是:
services.AddMediatR();
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
感谢您关注此事。按照您的建议注册行为可使Posthandler运行,但它会执行两次。任何ide为什么? – Larsi
是的,我编辑了我的答案。我发现注册处理器很奇怪,但不是相关的行为。我可能会在GitHub上打开一个问题。 –
只是你的后处理器没有执行?或处理程序?如果你能显示一些代码,它会有所帮助。 –
执行其他标准处理程序。 – Larsi