Search code examples
c#rabbitmqmasstransit

Custom message consumption in MassTransit


I have a requirement where I have two ASP.NET Core applications (A and B) that have the same code base. Each application publishes some commands that it consumes and handles.

Now, when Application A publishes a message, it is been consumed by both A and B as they both have the same consumers and same message types. I am trying to achieve the behavior where when application A publishes a message, the message should only be consumed by A and not by B.

I was wondering if it's possible to use the prefix used in

x.SetEndpointNameFormatter(new KebabCaseEndpointNameFormatter(prefix: "ApplicationA", includeNamespace: false));

Because each of A and B have a different prefix.


Solution

  • If I understood right, you are using masstransit with rabbitmq, maybe you can solve your requirement by configuring the exchangetype to direct exchange type.

    cfg.send<testmessage>(x => { x.useroutingkeyformatter(context => "routingkey"); });
    cfg.message<testmessage>(x => x.setentityname("testmessage"));
    cfg.publish<testmessage>(x => { x.exchangetype = exchangetype.direct; });
    cfg.receiveendpoint(host, "testmessage_queue", e =>
    {
        e.bindmessageexchanges = false;
        e.consumer<updatecustomerconsumer>();
        e.bind("testmessage", x =>
        {
            x.exchangetype = exchangetype.direct;
            x.routingkey = "routingkey";
        });
    });
    

    by using different routingkey for project A and project B your can consume the message accordingly in reuired projects.

    Code copied from here