Oleksandr Hrebeniuk
Oleksandr Hrebeniuk

Reputation: 165

How to config consume to work with different messages types?(MassTransit)

I'm using MassTransit and Azure Service Bus as transport. I have created IAlert and IAlertExt that implements IAlert. I'm trying to publish two messages:

busControl.Publish<IAlert>(new Alert(customerId));
busControl.Publish<IAlertExt>(new AlertExt(customerId));

And write consumers to handle messages:

public class AlertConsumer : IConsumer<IAlert>
    {
        public async Task Consume(ConsumeContext<IAlert> context){
           ...
        }
    }

public class AlertExtConsumer : IConsumer<IAlertExt>
    {
        public async Task Consume(ConsumeContext<IAlertExt> context){
            ...
        }
    }

Now after publish both consumers retrieve one message that was published with generic interface.

How to force AlertExtConsumer retrieve messages not only published with IAlertExt and also with IAlert?

Upvotes: 4

Views: 2456

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33542

You can put both interfaces in the same consumer, as shown below.

public class AlertConsumer : 
    IConsumer<IAlert>,
    IConsumer<IAlertExt>
{
    public async Task Consume(ConsumeContext<IAlert> context){
       ...
    }
    public async Task Consume(ConsumeContext<IAlertExt> context){
        ...
    }
}

Both interfaces will be bound to the same consumer queue.

Upvotes: 12

Related Questions