Search code examples
c#messagingrebus

Wait for ReBus event handler


Is there any mechanizm in Rebus which allows to specify action to invoke after speficic handler handles a message? In other words, callback?

I register hansler like this:

serviceCollection.AutoRegisterHandlersFromAssemblyOf<Handler1>()
serviceCollection.AddRebus(....);
var serviceProvider = serviceCollection.BuildProvider();
serviceProvider.UseRebus();




public class Handler1 : IHandleMessages<Message1>
{
   public Task Handle(Message1 message)
   {
       Console.WriteLine($"Handler1 received : {message}");
       return Task.CompletedTask;
   }
}

now, I would like to do something like this:

serviceProvider.GetService<IBus>(); //or whatever needed

//wait 2000ms until Handler1 handles Message1 with specific ID
await bus.WaitUntilHandled<Handler1, Message1>(
    message => message.Id = id, 
    new TaskCancellationSource(2000));

Solution

  • Rebus' built-in extension mechanism (for the message pipelines) lets you do almost anything you want to do, but in your case I think it would be easier to pull in Rebus.Events and do something like this:

    Configure.With(...)
        .(...)
        .Events(e =>
        {
            e.AfterMessageHandled += (bus, headers, message, context, args) =>
            {
                // do your thing in here
            };        
        });