A C# Programmer’s Guide to Installing, Running and Messaging with RabbitMQ

October 07, 2016

Following this year’s trip to DDD North I got speaking to someone about Rabbit MQ. I’d previously done some research on Active MQ, however, given that there are, realistically, only two options in this market, I thought I should have a look at the competition.

Install from here.

You also need to install Erlang.

After the install… nothing happens. No matter how long you wait, assuming it’s just your slow laptop (don’t want to talk about that anymore). Navigate to:


C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.0\sbin

In a DOS prompt, type:

rabbitmq-plugins enable rabbitmq_management


Rabbit will now run as a service, which also feels much cleaner that ActiveMQs console app style interface.

Now, log-in to http://localhost:15672/, and log-in as guest/guest:



Just like with ActiveMQ, there is a NuGet package that you can use for interacting with the exchange:

https://www.nuget.org/packages/RabbitMQ.Client. Your project needs .Net 4.5.1 or higher; otherwise, you’ll see this:


If it’s not, upgrade your project to 4.5.1:


Okay, so now we’re up and running, here’s the code for the send:

[code language=“csharp”] static void Main(string[] args) {                 while (true)     {         string msg = Console.ReadLine();         if (string.IsNullOrWhiteSpace(msg))         {             break;         }

        SendNewMessage(msg);     }       } private static void SendNewMessage(string message) {     var factory = new ConnectionFactory() { HostName = “localhost” };     using (var connection = factory.CreateConnection())     using (var channel = connection.CreateModel())     {         var result = channel.QueueDeclare(“NewQueue”, true, false, false, null);         Console.WriteLine(result);

        channel.BasicPublish("", “NewQueue”, null, Encoding.UTF8.GetBytes(message));                

    } }

Just like with ActiveMQ, you can now just look at the queue:


A quick caveat here:

[code language="csharp"]

var result = channel.QueueDeclare("NewQueue");

Doesn’t work because exclusive defaults to ‘true’, meaning that only your session can see it.

Receiving a Message

[code language=“csharp”]

static void Main(string[] args) {     while (true)     {         ReceiveNextMessage();

        string exit = Console.ReadLine();         if (!string.IsNullOrWhiteSpace(exit)) break;     }


private static void ReceiveNextMessage() {     var factory = new ConnectionFactory() { HostName = “localhost” };     using (var connection = factory.CreateConnection())     using (var channel = connection.CreateModel())     {         var result = channel.QueueDeclare(“NewQueue”, true, false, false, null);         Console.WriteLine(result);

        EventingBasicConsumer consumer = new EventingBasicConsumer(channel);         consumer.Received += Consumer_Received;

        channel.BasicConsume(“NewQueue”, true, consumer);


    } }

private static void Consumer_Received(object sender, BasicDeliverEventArgs e) {     var body = e.Body;     var message = Encoding.UTF8.GetString(body);     Console.WriteLine(message); }


Why the Thread.Sleep?  The reason is that, because the message consumer is event based, you need to stop tidying up the objects until you're done.  Obviously, sleeping for 1 second has its issues.  I suspect this could be better achieved using a [TaskCompletionSource](http://thelongwalk.tech/2016/06/01/taskcompletionsource/), of even just a Console.ReadLine(), but for the purposes of this post, it suffices.


The concept of topics seems to be the same as ActiveMQ; however, the implementation is different, and there were a few dead-ends to go down first.

The following code, for example:

[code language="csharp"]

var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
    channel.ExchangeDeclare("NewTopic", "testTopic");
    channel.BasicPublish("NewTopic", "test", null, Encoding.UTF8.GetBytes(message));

Gives the error: COMMAND_INVALID - unknown exchange type ‘testTopic’


RabbitMQ seems to have some magic strings that determine the message type. This works:

[code language=“csharp”]

private static void SendNewTopic(string message) { var factory = new ConnectionFactory() { HostName = “localhost” }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) {     channel.ExchangeDeclare(“NewTopic”, “topic”); // topic is a magic string          channel.BasicPublish(“NewTopic”, “test”, null, Encoding.UTF8.GetBytes(message));


The "test" is a routing code.  These seem to be quite an involved system; however, it basically tells the exchange where the message is going.



[code language="csharp"]

private static void ReceiveNextTopic()
    var factory = new ConnectionFactory() { HostName = "localhost" };
    using (var connection = factory.CreateConnection())
    using (var channel = connection.CreateModel())
        channel.ExchangeDeclare("NewTopic", "topic");
        var queueName = channel.QueueDeclare().QueueName;
        channel.QueueBind(queue: queueName,
                        exchange: "NewTopic",
                        routingKey: "test");
        EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
        consumer.Received += Consumer\_Received;
        channel.BasicConsume(queueName, true, consumer);

private static void Consumer\_Received(object sender, BasicDeliverEventArgs e)
    var body = e.Body;
    var message = Encoding.UTF8.GetString(body);

Notice the routing key; this (broadly speaking) needs to match the sender’s routing key. The purpose seems to be to allow a complex mapping between publisher and subscriber.


In comparison to ActiveMQ, RabbitMQ seems to be a more actively maintained code base; a quick comparison of the GitHub repo of each reveals more activity in Rabbit. Rabbit also seems a little cleaner in the way it’s run and installed; however, it also seems to have more moving parts.

There are a number of companies that offer support for ActiveMQ commercially; however, the company that wrote RabbitMQ offer commercial support, should you need it.

Can you run Active and RabbitMQ together?

No. No, you can’t. They both seem to want to use the same resources, and so if you want to use them both on the same machine, you can, but only one at a time.


I used the following websites extensively in this article:



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District

© Paul Michaels 2022