如果您只想影响消息是否应该发送到处理程序,那么 NServiceBus 提供了一个解决方案 =>DoNotContinueDispatchingCurrentMessageToHandlers()
您可以创建一个通用处理程序并将其设置为在任何其他处理程序之前触发。
public class SomeHandler: IHandleMessages<object>, ISpecifyMessageHandlerOrdering
{
public IBus Bus {get;set;}
public void Handle(object message)
{
Bus.DoNotContinueDispatchingCurrentMessageToHandlers();
}
public void SpecifyOrder(Order order)
{
order.SpecifyFirst<SomeHandler>();
}
}
有关更多详细信息,请参阅此答案
或者,可以将其作为 mutator 插入
class StopThePipelineMutator: IMutateIncomingTransportMessages,INeedInitialization
{
public IBus Bus { get; set; }
public void MutateIncoming(TransportMessage transportMessage)
{
Bus.DoNotContinueDispatchingCurrentMessageToHandlers();
}
public void Init()
{
Configure.Component<StopThePipelineMutator>(DependencyLifecycle.InstancePerCall);
}
}