RabbitMQ with C#
In this article, you will learn how to write a basic program using RabbitMQ and C# like how to create connections, how to publish a message in the queue and how to receive that message from a queue. Before writing code, I assume you have already had some idea about what is RabbitMQ and why we should use RabbitMQ. If not no worry here I am going to tell you some basic feature of RabbitMQ.
A Basic Introduction of RabbitMQ
- It is quite popular, free and open Source message broker that implements AMQP(Advanced Message Queuing Protocol).
- It is written in Erlang programming language. You don’t need to learn Erlang to go through with RabbitMQ. Erlang is just a prerequisite because it is written in Erlang and it needs that to run in your system.
- It allows you not to worry about programming Interoperability. Publisher and Receiver are independent to choose their own programming language as per their interest.
- It is easy to use and can run on many different OS (Operating System)
- It supports various messaging patterns.
- Producer: Producer is a component that sends or can say push data to message broker in Queue.
- Exchange: Exchange is a component which comes in action when a producer creates a message that will not directly be sent to a queue, instead first the message will be sent to exchanges, then after that a routing agent reads and sends it to the appropriate queue with help of header attributes, bindings, and routing keys.
- Queue: Queue is a component which acts like message buffer which can hold a large amount of data. It works on the basis of FIFO (First in First Out
- Consumer: Consumer is a component which basically consuming or can say receiving data stored in Queue
For more Information, I suggest you read their official documentation at https://www.rabbitmq.com/getstarted.html
Installation and Setup
It is quite easy for the installation of RabbitMQ application on your system. Please follow the given steps in order to install RabbitMQ in your system.
- Download Erlang from their official Website. http://www.erlang.org/downloads
- Install Erlang installer in your system.
- Download RabbitMQ installer from their official website. https://www.rabbitmq.com/install-windows.html
- Install RabbitMQ installer in your System.
Great you have successfully installed RabbitMQ in your system. You might be excited to open RabbitMQ in your browser to see how its User Interface looks and what things you can do. Still, one step is left and that is to enable RabbitMQ management.
Fine! but how to do?
Open RabbitMQ Command Prompt as an administrator mode.
- Navigate to the sbin directory of the RabbitMQ Server installation directory. Probable path is C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-3.3.4\sbin
- Run the following command to enable the plugin
Have a look at below images to see step by step process in order to install and run RabbitMQ in your system.
Installing Erlang installer in your system
Installing RabbitMQ Server 3.7.10 Setup
Enabling RabbitMQ Management
RabbitMQ Localhost Url – localhost:15672
RabbitMQ Management UI
Programming in RabbitMQ using C#
As you have already installed RabbitMQ in your system and have checked it is working fine. Now, this is time to write a program and publish a message in Queue and Receive that message from Queue. In order to perform all these tasks, you need RabbitMQ client in your visual studio to connect RabbitMQ. You can easily install RabbitMQ Client by using Package Manager Console.
Code to Publish Message in Queue
You have to use RabbitMQ.Client as the namespace in order to use all available classes and functions to write code for creating a connection with RabbitMQ, create exchange, queue and publish message. ConnectionFactory is the main entry point to the RabbitMQ.NET AMQP client. It constructs the IConnection instances. Define all required property of ConnectionFactory like HostName, UserName, Password, Port. CreateConnection is used to create a connection to the one of the endpoint. CreateModel is used to create and return fresh channel, session, and model. ExchangeDeclare is used to declare an exchange. Here we are using AMQP fanout exchanges. In the later article, we will discuss the different type of exchange type. QueueDeclare is used to declare a Queue. QueBind is used to bind a queue with the exchange. BasicPublish is used to publish your message to Queue.
Once we write code to publish a message in Queue. Run the program and see its effect on RabbitMQ management. Navigate to http://localhost:15672 and you can see there are one extra exchange have been created named “HelloWorld_RabbitMQ”
You can also see a new entry of Queue named “HelloQueue” having one message to read.
When you click on HelloQueue and it will navigate to HelloQueue page where you can see what message you have published.
Code to Consume Message from Queue
As you can see, we are going to write the same code as we have written in the Producer class in order to create a connection, declare exchange and queue.
At last, we are going to tell the server to deliver the messages from the queue what we have published. Since it will push us messages asynchronously, we provide a callback. That is what EventingBasicConsumer.Received event handler does.
Once you run the program you can see the message you have published in the Queue is being displayed on your screen.
Just to demonstrate RabbitMQ I have used the same host to send and receive the message. Usually, it doesn’t happen. In Later article, we will cover some more interesting topic of RabbitMQ till then keep learning. Happy Coding 🙂