Mass Transit and Azure service bus: message consumed multiple times by different dev spaces

533 Views Asked by At

I'm publishing a message after registering in this way (comments line are many attempts done). I pass a queuePrefix in order to use different dev spaces, one per developer, since the application is microservices based. Masstransit decouples the information exchange by different microservices

           this IServiceBusBusFactoryConfigurator cfg, IServiceProvider provider, string queuePrefix = null,
           Action<IConsumerConfigurator<TConsumer>> configure = null)
           where TConsumer : class, IConsumer<TCommand>
           where TCommand : class
        {

            string queueName = $"{queuePrefix}-{typeof(TCommand).Name}";
            cfg.Message<TCommand>(configureTopology => {
                //configureTopology.SetEntityName(queueName);
                //configureTopology.UsePartitionKeyFormatter(context => context.Message.CustomerId);
            });
            cfg.Publish<TCommand>(configureTopology => {
                configureTopology.EnablePartitioning = true;
                //configureTopology.UsePartitionKeyFormatter(context => context.Message.CustomerId);
            });
            cfg.Send<TCommand>(x =>
            {
                //x.UsePartitionKeyFormatter(context => context..Message.CustomerId);
            });
            cfg.ReceiveEndpoint(queueName, endpointCfg =>
            {                
                endpointCfg.RemoveSubscriptions = true;                
                endpointCfg.EnablePartitioning = true;
                endpointCfg.Consumer<TConsumer>(provider, configure);
            });
            return cfg;
        }

I though the prefix was enough, but if I publish a message, all the dev spaces consume the message. What should I do? I've tried subscription without success. It seems I've taken the wrong path

1

There are 1 best solutions below

5
On BEST ANSWER

The reason is that each message type has the same topic name, and all dev spaces are subscribing to the same topics. You can override the topic names similar to how you override the queue names to include a prefix by creating your own IEntityNameFormatter, similar to how it was done in this question/answer.

MassTransit includes an entity name formatter, PrefixEntityNameFormatter, which was added in v7, that does the same thing, you just need to specify it on your bus configuration for message topology.

cfg.MessageTopology.SetEntityNameFormatter(
    new PrefixEntityNameFormatter(cfg.MessageTopology.EntityNameFormatter, "Frank-"));