如何在NServiceBus 6中的"Handle"方法之后注册要执行的行为?

Ada*_*lin 0 nservicebus nservicebus6 nservicebus-pipeline

我有Endpoint一个Handle方法.我想在之前和之后立即采取措施Handle.通过改变,我之前能够完成这个步骤LogCommandEntryBehavior : Behavior<IIncomingLogicalMessageContext>.紧接着需要实施什么Handle

Mik*_*llo 7

在NServiceBus版本6中,管道由一系列阶段组成,每个阶段都嵌套在前一个阶段,就像一套俄罗斯娃娃.对于传入消息,阶段是(按顺序):

  • ITransportReceiveContext,
  • IIncomingPhysicalMessageContext,
  • IIncomingLogicalMessageContext,和
  • IInvokeHandlerContext

在阶段中创建行为时,会传递一个名为的委托next().当您调用时next(),执行管道中的下一个行为(可能会将管道移动到下一个阶段).next()返回a 的调用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.
    }
}
Run Code Online (Sandbox Code Playgroud)

如果要记录有关处理消息的信息,我建议您查看该IInvokeHandlerContext阶段.它包含有关如何处理消息的信息,并将为每个被调用的处理程序调用一次(如果您有多个处理程序).如果您不需要该级别的信息,那么IIncomingLogicalMessageContext可能就是您所需要的.

您可以在特定文档站点中阅读有关版本6管道的更多信息: