'MassTransit - Assign consumers to two different endpoints when the consumers are resolved via container

I'm using MassTransit 7.3.0 with Autofac and RabbitMq.

So far, I was content for all my services to use a single queue/endpoint per service.

I registered my consumers basically like this:

builder.AddMassTransit(massTransit =>
{
    mt.AddConsumers(consumerAssembly);

    massTransit.UsingRabbitMq((ctx, cfg) =>
    {
        cfg.Host(config.HostName, "/", h =>
        {
            h.Username(config.Username);
            h.Password(config.Password);
        });
        cfg.ReceiveEndpoint(logicModule.InputQueue, ep => ep.ConfigureConsumers(ctx));
    });
});

Now however I need each service to have 2 queues, where the first queue is for most consumers except a few special ones, and the second is for those special ones; the second queue also needs to have a few settings applied (Durable , PurgeOnStartup and AutoDelete) which I cannot set via a consumer definition.

However, I don't see how I can

a) exclude the special consumers from being added to the first queue (because AddConsumers will take just all) and the regular one from being added to the second queue

b) register the special consumers to the second queue while still using the container to resolve them

UPDATE: I noticed that IContainerBuilderBusConfigurator.AddConsumer() has a method .Endpoint() which allows limited configuration of the endpoint. So I tried moving the special consumers into a different assembly (so they are not added with the mt.AddConsumers(consumerAssembly) line) and adding them manually like

mt.AddConsumer<MySpecialConsumer>().Endpoint(ep => ep.Name="myspecialqueue");

and then set up the second queue like this:

cfg.ReceiveEndpoint("myspecialqueue", ep =>
{
   ep.ConfigureConsumers(ctx);
   ep.AutoDelete = true;
   ep.PurgeOnStartup = true;
   ep.Durable = false;
});

However, when I look into RabbitMq, I still see the consumers bound to the first queue (logicModule.InputQueue) instead of the second (myspecialqueue).

I assume this is because the setup of the first queue (cfg.ReceiveEndpoint(logicModule.InputQueue, ep => ep.ConfigureConsumers(ctx));) already adds them. However, there seems to be no way to exclude them. Or this path is not the right one.



Solution 1:[1]

Why don't you add an integer role or any type of role to make the consumer special. For example you can add a ne table into the database. Name the table Roles. Two preset variables are in this table :"Normal"&"Special". Normal=1, Special=2. When consumer is saved assign either 1 if its a normal or 2 if its a special. Thus being a foreign key primary key relationship with identifying values.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thaveshan Naidoo