In the development of distributed applications, one of the main needs is efficient communication between different services. One of the most powerful tools for this purpose is RabbitMQ, a message broker that facilitates the exchange of messages between applications in a robust and scalable way. In this article, I will show you how to integrate RabbitMQ with a .NET Core application.
What is RabbitMQ?
RabbitMQ is an open-source message broker that implements the AMQP (Advanced Message Queuing Protocol). It allows applications to send and receive messages asynchronously, decoupling communication between different parts of the system.
Why Use RabbitMQ with .NET Core?
- Performance and Scalability: RabbitMQ allows you to handle large volumes of messages efficiently.
- Decoupling: Facilitates the construction of distributed and decoupled systems.
- Reliability: Offers mechanisms for message persistence and acknowledgment.
Setting Up RabbitMQ
Before you start, you need to have RabbitMQ installed and configured. You can download RabbitMQ from the official website and follow the installation instructions.
Creating a .NET Core Application
1. Create a new .NET Core project:
dotnet new console -n RabbitMQExample
cd RabbitMQExample
2. Add the RabbitMQ.Client package:
dotnet add package RabbitMQ.Client
Sending Messages with RabbitMQ
Let’s create a producer that sends messages to a RabbitMQ queue.
Producer code:
public static async Task Execute(Message message)
{
var factory = new ConnectionFactory
{
HostName = "",
UserName = "",
Password = "",
Port = 0,
VirtualHost = "",
};
using (var connection = await factory.CreateConnectionAsync())
{
using (var channel = await connection.CreateChannelAsync())
{
await channel.QueueDeclareAsync(queue: "Q_NAME",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
var json = JsonConvert.SerializeObject(message);
var body = Encoding.UTF8.GetBytes(json);
await channel.BasicPublishAsync(exchange: "", routingKey: "R_KEY", body: body);
}
}
}
Consuming Messages with RabbitMQ
Let’s create a consumer that receives messages from RabbitMQ queue.
Consumer code:
public static async Task<Message> Execute()
{
var message = new Message();
var factory = new ConnectionFactory
{
HostName = "",
UserName = "",
Password = "",
Port = 1,
VirtualHost = ""
};
using (var connection = await factory.CreateConnectionAsync())
{
using (var channel = await connection.CreateChannelAsync())
{
await channel.BasicQosAsync(0, 0, false);
var queueInfo = await channel.QueueDeclareAsync(queue: "Q_NAME",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new RabbitMQ.Client.Events.AsyncEventingBasicConsumer(channel);
var messageCount = queueInfo.MessageCount;
if (messageCount == 0)
return message;
consumer.ReceivedAsync += async (model, ea) =>
{
try
{
var body = ea.Body.ToArray();
var json = Encoding.UTF8.GetString(body);
var message = JsonConvert.DeserializeObject<Message>(json);
if (message is not null)
{
await channel.BasicAckAsync(ea.DeliveryTag, multiple: false);
}
else
{
await channel.BasicRejectAsync(ea.DeliveryTag, requeue: true);
}
}
catch (Exception)
{
//Log
await channel.BasicRejectAsync(ea.DeliveryTag, requeue: true);
}
};
await channel.BasicConsumeAsync(queue: "Q_NAME", autoAck: false, consumerTag: "C Tag", consumer: consumer);
}
return message;
}
}
Conclusion
In this article, we saw how to set up and use RabbitMQ with .NET Core by creating a producer and a consumer of messages. This is just the basics, RabbitMQ offers many other features, such as exchanges, topics, durable queues, and transactions that can be explored to meet the specific needs of your application.
I hope this guide has been helpful and that you can implement robust and scalable solutions in your .NET Core applications using RabbitMQ. If you have any questions or want to share your experience, leave a comment!
Feel free to share this article on your LinkedIn, x , facebook or other platform and follow me for more tips on development and technologies!



