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

she*_*nku 1 nservicebus nservicebus5 nservicebus6

在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;
Run Code Online (Sandbox Code Playgroud)

第6节中的代码:

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

Mik*_*llo 6

IOutgoingSendContext是在管道捕捉物理目标为时尚早.每个传出发送操作将在NServiceBus版本6中经历以下上下文(按顺序):

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

之后IOutgoingSendContext的路由策略选择,但它不转换成物理地址,直到以后IRoutingContext.

出于这个原因,如果你想跟踪物理地址,最好的选择是坐在IDispatchContext.该上下文将包含TransportOperations 的集合,每个s都有一个AddressTag.这要么是实例UnicastAddressTagDestination或实例MulticastAddressTagMessageType.

以下是一些可以帮助您入门的代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

有关NServiceBus版本6管道的更多信息,请参阅NServiceBus文档中的步骤,阶段和连接器.