3
votes

I have a situation where I need to pause message processing in my application instance that is using Rebus with Azure Service Bus.

The lifecycle of the application configures the IoC container at application startup, then it configures and starts Rebus when the application is ready to start processing messages, which automatically adds Rebus and its dependencies to the IoC container.

At a later stage in the application lifecycle it might need to pause message processing, and then later on start message processing again, without the application being restarted.

In my current experiment I try to dispose the IBus when I need to pause the message processing, but I don't dispose the IoC container (Windsor), since other components need it during the pause. The result I get is an InvalidOperationException

System.InvalidOperationException 
Attempted to register primary -> Rebus.Config.Options, but a primary registration already exists: primary -> Rebus.Config.Options
   at Rebus.Injection.Injectionist.Register[TService](Func`2 resolverMethod, Boolean isDecorator, String description)
   at Rebus.Config.RebusConfigurer.Start()

when reconfiguring and starting the bus again reusing the same container instance. The exception seems to occur due to some of the Rebus dependencies are already registered in the container. So I need some other way to halt the message processing without disposing the IBus instance.

Is it at all possible to pause/start message processing during the lifetime of an application? Looking at the IBus api or the IAdvancedApi I cannot find a proper method to achieve this.

1

1 Answers

2
votes

It's pretty easy, when you know it 🙂: You can simply set the number of working threads to 0, which will stop message processing:

// blocks until all currently executing message handlers have finished
// what they're currently up to
bus.Advanced.Workers.SetNumberOfWorkers(0);

and then you resume message processing again by adding them back:

// you should probably get the number from a configuration
// value somewhere
bus.Advanced.Workers.SetNumberOfWorkers(3);

👍