In c#, we can read or consume messages from rabbitmq by using RabbitMQ.Client NuGet package. Here, we will learn how to consume or get messages from rabbitmq using RabbitMQ.Client service in c# or .net application with examples.
Let’s create a simple console application with the Name “RabbitMQConsumer” as shown below to consume or read messages from the rabbitmq queue.
After creating an application, now we will add “RabbitMQ.Client” NuGet package in our c# application to communicate with rabbitmq server to publish or consume messages from queues in rabbitmq.
In c#, we need to add the “RabbitMQ.Client” NuGet package in our application to communicate with RabbitMQ server to publish or consume messages for that, right click on your application and select Manage NuGet Packages like as shown below.
Now search for RabbitMQ.Client package and install it in your application as shown below.
Following is another way to install RabbitMQ.Client package by using Package Manager Console as shown below by entering the following command.
Command to install - Install-Package RabbitMQ.Client -Version 5.1.0
After installing RabbitMQ.Client, next add a class with the name “MessageReceiver.cs” in your application as shown below.
Now, open MessageReceiver.cs class file and write the code as shown below.
If you observe the above code, we created a MessageReceiver class and it’s inheriting from DefaultBasicConsumer class of RabbitMQ.Client service and we implemented a HandleBasicDeliver method by overriding it to receive a message body.
Now open Program.cs class file and write a code in the Main method to call MessageReceiver.cs class to get messages from rabbitmq.
Following is the code which we need to write in Program.cs class file Main method to receive data from rabbitmq server.
If you observe the above example, to establish a connection with the rabbitmq server we are passing the required credentials along with the HostName to ConnectionFactory() method. After that, we created a connection and channel by calling the “CreateConnection” and “CreateModel” methods and we set a prefetchCount to 1, so that it tells RabbitMQ not to give more than one message at a time to worker.
Next, we created an instance of MessageReceiver class and passed IModel (channel) to it, in the final step we called the “BasicConsume” method and passed the queue name to it “demoqueue” along with we have set autoAck to false and passed the messageReceiver instance to it.
Here, prefetchCount is used to tell RabbitMQ not to give more than one message at a time to worker. Or, in other words, don't dispatch a new message to a worker until it has been processed and acknowledged the previous one. Instead, it will dispatch to the next worker that is not still busy.
Now we are done with the creation of rabbitmq consumer application. Before we start reading the data from the rabbitmq server, first we need to publish the message to the rabbitmq queue (demoqueue) for that check the C# Publish Messages to RabbitMQ Queue article.
After we publish data to rabbitmq queue (demoqueue) that will be like as shown below.
After publishing the messages to the queue run your c# consumer application to read messages from rabbitmq and that will return the result as shown below.
This is how we can consume or read messages from rabbitmq queues in c# using RabbitMQ.Client service based on our requirements.