1
votes

I want to be able to write custom middleware for Rebus which will wrap around message handlers: executing before and after each handler, regardless of the message type. Is this possible with Rebus, and if so, how do I do that?

Browsing the source code of Rebus, I think it should be possible, as the library is very neatly built around the concept of IPipeline with customizable "steps". But although it looks like it should be easy to add custom steps, I cannot find any public API exposing the pipline. Is this possible to modify the pipeline from the client code?

What I am looking for is essentially an equivalent to MassTransit's IFilter<>. We are about to choose between MassTransit and Rebus (or possibly a custom implementation on top of Microsoft.Azure.ServiceBus) for a new project, and at this moment this looks like a major feature missing from Rebus.

1

1 Answers

2
votes

With Rebus, you add custom steps by decorating IPipeline, which makes it possible to change both incoming and outgoing messages pipelines however you want.

To make it more convenient to inject steps into a specific position in the pipeline, Rebus comes with a PipelineStepInjector, which is a decorator that enables positioning the injected step relative to another step.

Check out the Pipelines section on the wiki page about extensibility – it shows how an outgoing step can be used to automatically enrich the headers of all outgoing messages.

Incoming steps can be added in a similar manner, only using the OnReceive method instead of OnSend to add the step:

Configure.With(...)
    .(...)
    .Options(o => {
        o.Decorate<IPipeline>(c =>
        {
            var pipeline = c.Get<IPipeline>();
            var step = new YourIncomingStep();

            return new PipelineStepInjector(pipeline)
                .OnReceive(step, PipelineRelativePosition.Before, typeof(DispatchIncomingMessageStep));
        });
    })
    .Start();