Handler pipeline

Dave Anderson edited this page Jun 10, 2018 · 8 revisions

When Rebus handles an incoming message, it constructs a handler pipeline. Why is that?

Well, first of all: Your IHandlerActivator may return multiple handler instances when asked to activate handlers for a particular message type. E.g. you might have two things happening upon receiving a CustomerMoved event:

public class UpdateCustomerInformation : IHandleMessages<CustomerMoved> { ... }

and

public class InitiateDistrictChangedCheck : IHandleMessages<CustomerMoved> { ... }

But then your CustomerMoved event looks like this:

public class CustomerMoved : GeneralCustomerChange { ... }

and you actually have a handler that will sometimes notify some customer handling department of certain things that relate to when general stuff changes on a customer:

pubic class NotifyCustomerDepartmentSometimes : IHandleMessages<GeneralCustomerChange> { ... }

and since Rebus does polymorphic message dispatch, you now have three handlers to be executed on an incoming CustomerMoved event.

Now, what if you NEED to update customer information before doing anything else? I.e. execute the UpdateCustomerInformation handler before the others? With Rebus, it's easy - just set an order of the handlers like this:

Configure.With(...)
    .Options(o => {
        o.SpecifyOrderOfHandlers(o => o.First<UpdateCustomerInformation>());
    })
    //etc

which will fit a special implementation of IInspectHandlerPipeline that orders the handlers before the message gets dispatched. If you need to, you can specify the order of multiple handlers, which - if they occur in the handler pipeline - will be guaranteed to be first and respecting the specified order:

Configure.With(...)
    .Option(o => {
        .SpecifyOrderOfHandlers(o => o.First<ThisHandler>()
                                      .Then<AnotherHandler>()
                                      .Then<YetAnotherHandler>())
    })
    //etc

Please note that you cannot specify each handler more than once, and you cannot call SpecifyOrderOfHandlers more than once. If you need more complex logic, you can make your own implementation of IInspectHandlerPipeline, which may even add and remove handlers at will.

If you're interested in reading more about this, check out the section on Message dispatch.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.