Few people have asked me already how to start using RabbitMQ in .NET, so I decided to write a short post about it.
First, visit erlang’s page, download erlang distribution according to your system (either 32 or 64 bit) and install it. If you have 64 bit machine (and you really should have) than get the 64 bit distribution, otherwise erlang process won’t be able to address more than 2GB of memory.
Next download RabbitMQ server and install it. I strongly recommend to install Management Plugin, which comes with RabbitMQ installation. All you need to do is run following command from command prompt (you may have to navigate to RabbitMQ’s sbin folder):
rabbitmq-plugins enable rabbitmq_management
There are detailed instructions on Management Plugin and its installation at http://www.rabbitmq.com/management.html.
Although pivotal provides a great .NET library for working with RabbitMQ, there is even better solution which will be sufficient in most cases – EasyNetQ. EasyNetQ provides nice abstraction over some low level concepts of RabbitMQ, such as topology registration, messages serialisation or error handling. It also gives an easy interface to work with some popular messaging patters such as Publish / Subscribe or Message Bus.
Lets create our first application. Start new console application project in Visual Studio and name it
EasyNetQ nuget package:
Main.cs code with following:
Now, add new console application project to the solution and call it
ProducerApplication. You also have to add
EasyNetQ nuget package to this project.
Main.cs code with following:
To run the example, first right click on the ConsumerApplication project and select Debug > Start new instance. This will run the ConsumerApplication. Next, do that same on the PublisherApplication and you should see that in the ConsumerApplication window there is a lot of diagnostics information printed and right at the bottom there is the
Hello World! message.
How it works
Under the bonnet EasyNetQ is doing quite a work to make all this happen. When publishing the message, EasyNetQ performs a check to see whether the related Exchange exists in RabbitMQ and if it doesn’t than creates it. On the other hand, call to Subscribe method will verify whether both exchange and queue exists and they are bound together. As you can see in the console windows, EasyNetQ runs quite verbose diagnostics which are priceless when it comes to debugging. If there was a problem with consuming the message, such as exception thrown by consumer code, then it will be automatically moved to error queue for further analysis. You can also rely on EasyNetQ to manage connections to RabbitMQ server so you do not need to worry when the connection was dropped etc.
As you could see from the above example it is very easy to start with messaging using combination of RabbitMQ and EasyNetQ.
From serialising / de-serialising messages, diagnostics and error handling, managing connection to managing topology – all handled automatically by EasyNetQ so you can concentrate your efforts on making awesome applications. As you will see in the second article, working with complex type messages is no more complicated.