This article contains some instructions to make it easy for people to jump into the wonderful world of message queues with RabbitMQ. Its purpose is not to provide any in-depth explanations, as I’m pretty new to the topic myself. 🙂
RabbitMQ is built on Erlang, so you’ll need to install that first:
You can then download and install RabbitMQ itself:
Next, you should install the management plugin. This provides a web UI allowing you to manage your queue and perform basic testing (e.g. publish messages).
To do this, navigate to the folder where you installed RabbitMQ, and go into the sbin folder. From here, enter the following command:
rabbitmq-plugins enable rabbitmq_management
Once that is installed, you can access the administrative web UI by going to http://localhost:15672/ with username guest and password guest. Go to the Queues tab and create a new queue:
You’ll now see the new queue listed under “All queues”. Click on the name to select it. This will allow you to view information about the queue (including statistics, such as the number of messages in the queue) and perform a number of operations (including publishing messages).
But before we do that, let’s create a simple client in .NET that can subscribe to the queue and consume any messages we publish. Some basic code is available in this tutorial, so we can just install the RabbitMQ.Client NuGet package and then write some code based on it:
static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = "localhost" }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { channel.QueueDeclare("testqueue", true, false, false, null); var consumer = new EventingBasicConsumer(channel); consumer.Received += Consumer_Received; channel.BasicConsume("testqueue", true, consumer); Console.ReadLine(); } } } private static void Consumer_Received(object sender, BasicDeliverEventArgs e) { var body = e.Body; var content = Encoding.UTF8.GetString(body); Console.WriteLine(content); }
We can now run this client, and leave it waiting for messages.
To test it, let’s publish a message from the RabbitMQ management web UI:
You can see from the client that the message has been received:
So that shows in a very basic way how you can setup RabbitMQ on Windows, publish messages from the management web UI, and consume messages by writing a simple .NET client.
Update 2nd March 2016: If the management plugin fails to install with something like “unable to contact node”, here are a few things you can try:
- Sync up your Erlang cookies
- Reinstall the RabbitMQ service
- Restart RabbitMQ server with the command
rabbitmq-server restart
. If this fails with an error like “Cluster upgrade needed”, it is most likely because of baggage left behind from a previous install. Go into %APPDATA% and delete the RabbitMQ folder there, then reinstall RabbitMQ from the installer.
7 thoughts on “Getting Started with RabbitMQ with .NET”