简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM