在 NServiceBus 6 中如何注册一个行为在 "Handle" 方法之后执行?
How do you register a behavior to execute after the "Handle" method in NServiceBus 6?
我有一个 Endpoint
和 Handle
方法。我想在 Handle
之前和之后立即做一些事情。我之前通过 imolementing LogCommandEntryBehavior : Behavior<IIncomingLogicalMessageContext>
能够使步骤正常工作。在 Handle
之后需要立即实施什么?
在 NServiceBus 版本 6 中,管道由一系列阶段组成,每个阶段嵌套在前一个阶段中,就像一组 Russian Dolls。对于传入消息,阶段是(按顺序):
ITransportReceiveContext
,
IIncomingPhysicalMessageContext
,
IIncomingLogicalMessageContext
,以及
IInvokeHandlerContext
当您在一个阶段中创建一个行为时,您将获得一个名为 next()
的委托。当您调用 next()
时,您将执行管道中的下一个行为(这可能会将管道移动到下一个阶段)。对 next()
returns 和 Task
的调用指示管道的内部部分何时完成。
这让您有机会在进入下一阶段之前调用您的代码,并在下一阶段完成后调用更多代码,如下所示:
public class LogCommandEntryBehavior : Behavior<IIncomingLogicalMessageContext>
{
public override async Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
{
// custom logic before calling the next step in the pipeline.
await next().ConfigureAwait(false);
// custom logic after all inner steps in the pipeline completed.
}
}
如果您想记录有关消息处理的信息,我建议查看 IInvokeHandlerContext
阶段。它包含有关如何处理消息的信息,并且将为调用的每个处理程序调用一次(如果您有多个处理程序)。如果您不需要该级别的信息,那么 IIncomingLogicalMessageContext
可能就是您所需要的。
您可以在特定文档站点中阅读有关版本 6 管道的更多信息:
我有一个 Endpoint
和 Handle
方法。我想在 Handle
之前和之后立即做一些事情。我之前通过 imolementing LogCommandEntryBehavior : Behavior<IIncomingLogicalMessageContext>
能够使步骤正常工作。在 Handle
之后需要立即实施什么?
在 NServiceBus 版本 6 中,管道由一系列阶段组成,每个阶段嵌套在前一个阶段中,就像一组 Russian Dolls。对于传入消息,阶段是(按顺序):
ITransportReceiveContext
,IIncomingPhysicalMessageContext
,IIncomingLogicalMessageContext
,以及IInvokeHandlerContext
当您在一个阶段中创建一个行为时,您将获得一个名为 next()
的委托。当您调用 next()
时,您将执行管道中的下一个行为(这可能会将管道移动到下一个阶段)。对 next()
returns 和 Task
的调用指示管道的内部部分何时完成。
这让您有机会在进入下一阶段之前调用您的代码,并在下一阶段完成后调用更多代码,如下所示:
public class LogCommandEntryBehavior : Behavior<IIncomingLogicalMessageContext>
{
public override async Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
{
// custom logic before calling the next step in the pipeline.
await next().ConfigureAwait(false);
// custom logic after all inner steps in the pipeline completed.
}
}
如果您想记录有关消息处理的信息,我建议查看 IInvokeHandlerContext
阶段。它包含有关如何处理消息的信息,并且将为调用的每个处理程序调用一次(如果您有多个处理程序)。如果您不需要该级别的信息,那么 IIncomingLogicalMessageContext
可能就是您所需要的。
您可以在特定文档站点中阅读有关版本 6 管道的更多信息: