Publish message using exchange and routing key using MassTransit

Syed picture Syed · May 10, 2015 · Viewed 8.2k times · Source

I've been looking at MassTransit for a couple of weeks now and I'm curious about the possibilities. However, I don't seem to be able to get the concepts quite right.

Expected behaviour I wanted to publish message to "direct" exchange with routing key which is bind to two different queue for performing other activities.

When I attempted the same logic using MassTransit for better scalability. I've found MassTransit creates own exchange based on queue name with fanout type.

Classic code to publish message by exchange and routing key

using (var connection = factory.CreateConnection())
        {
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange, "direct");

                var body = Encoding.UTF8.GetBytes(message);

                channel.BasicPublish(exchange, routingKey, null, body);
                Console.WriteLine(" [x] Sent {0}", message);
            }
        }

Is there a way to configure direct or topic exchange with routingkey in MassTransit?

Answer

Travis picture Travis · May 10, 2015

This is not a supported scenario with MassTransit. MassTransit will always create a fanout queue. If you managed your topology yourself, you can use IEndpoint.Send to directly send the message to the exchange you created. In this case you give up much what MT provides though.

I'm also not sure what "better scalability" means in this case. Fanout exchanges perform better than direct exchanges (in most cases) since there's no routing logic that needs to be processed.

Maybe if you clarified your performance concerns on the MassTransit mailing list we could help you a bit more there.