Handler Ordering

Component: NServiceBus
NuGet Package NServiceBus (7-pre)
This page targets a pre-release version and is subject to change prior to the final release.

Multiple classes may implement IHandleMessages<T> for the same message. In that scenario, all the handlers will execute in the same transaction scope. These handlers can be invoked in any order. Handler Ordering allows the the order of execution of Handlers to be specified in code.

How it is actually implemented

  1. Find the list of possible handlers for a message.
  2. If order has been specified for any of those handlers move them at the start of the list.
  3. Execute the handlers.

The inference here is that the remaining handlers (not specified in the order) are executed in a non-deterministic order.

With the configuration API

endpointConfiguration.ExecuteTheseHandlersFirst(
    typeof(HandlerB),
    typeof(HandlerA),
    typeof(HandlerC));

Specifying one to run first

endpointConfiguration.ExecuteTheseHandlersFirst(typeof(HandlerB));

Specifying multiple to run ordered

endpointConfiguration.ExecuteTheseHandlersFirst(
    typeof(HandlerB),
    typeof(HandlerA),
    typeof(HandlerC));

Last modified