1

我有Endpoint一个Handle方法。我想在之前和之后立即做一些事情Handle。我能够通过 imolementing 使之前的步骤正常工作LogCommandEntryBehavior : Behavior<IIncomingLogicalMessageContext>。紧随其后需要实施什么Handle

4

1 回答 1

8

在 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.
    }
}

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

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

于 2017-02-27T02:54:19.923 回答