Search code examples
rebusrebus-azureservicebus

Rebus with Azure Service Bus, subscribe to an interface


I'd like to receive all the messages that are implementing a specific interface:

public interface IBusItem
{
    Guid BusItemId { get; }
    DateTime Timestamp { get; }
}

For example, I have a message like this:

public class SomeMessage : IBusItem
{
    public Guid BusItemId { get; set; } = Guid.NewGuid();
    public DateTime Timestamp { get; set; } = DateTime.UtcNow;
    public string Message { get; set; }
}

I have an handler for SomeMessage and another one for IBusItem. If in the receiver I do:

bus.Subscribe<SomeMessage>();

then BOTH handlers are called.

But if I want to subscribe just to IBusItem:

bus.Subscribe<IBusItem>();

then no handler is called

Is it possible to subscribe to a base interface/class using Rebus?


Solution

  • Is it possible to subscribe to a base interface/class using Rebus?

    Unfortunately, no.

    While Rebus has polymorphic dispatch, this only applies to when a message has been received and is to be dispatched to handlers.

    So if you want to receive all concrete types that implement IBusItem, you need to call await bus.Subscribe<ConcreteBusItem>() for each of them.