如何查看 nserviceBus 消息的目的地?

How can I see the destination of an nserviceBus message?

在 nServiceBus 的第 5 版中,我有一个行为可以跟踪正在传输的消息。

在行为中,我能够访问 DeliveryOptions(SendOptions) 并查看目标队列,在 NSB 6 中,随着对行为的更改,我似乎无法再访问消息的目标。

有谁知道从行为访问传出消息的目的地?

v5 中的先前代码:

 public class PendingCommandBehavior : IBehavior<OutgoingContext>
 {
        public void Invoke(OutgoingContext context, Action next)
        {
            var sendOptions = context.DeliveryOptions as Nsb.Unicast.SendOptions;
            if (sendOptions != null && context.OutgoingMessage.MessageIntent == Nsb.MessageIntentEnum.Send)
            {
                var destinationEndpoint = sendOptions.Destination.Queue;

v6 中的代码:

 public class PendingCommandBehavior : Behavior<IOutgoingSendContext>
    {
        public override async Task Invoke(IOutgoingSendContext context, Func<Task> next)
        {
            // context doesn't have any destination queue information???

IOutgoingSendContext 在管道中为时过早,无法捕获物理目标。在 NServiceBus 版本 6 中,每个传出发送操作将通过以下上下文(按顺序):

  • IOutgoingSendContext
  • IOutgoingLogicalMessageContext
  • IOutgoingPhysicalMessageContext
  • IRoutingContext
  • IBatchDispatchContext(如果您从消息处理程序内部发送)
  • IDispatchContext

IOutgoingSendContext 之后选择了路由策略,但直到 IRoutingContext 之后才将其转换为物理地址。

因此,如果您想跟踪物理地址,最好的办法是坐在 IDispatchContext 中。此上下文将包含 TransportOperation 的集合,每个集合都有一个 AddressTag。这将是带有 DestinationUnicastAddressTag 的实例或带有 MessageType.

MulticastAddressTag 的实例

这里有一些代码可以帮助您入门:

public override Task Invoke(IDispatchContext context, Func<Task> next)
{
    foreach (var operation in context.Operations)
    {
        if (operation.AddressTag is UnicastAddressTag unicastAddressTag)
        {
            var destinationEndpoint = unicastAddressTag.Destination;
        }
    }

    return next();
}

有关 NServiceBus 版本 6 管道的详细信息,请参阅 NServiceBus 文档中的 Steps, Stages and Connectors