.NET Core - RabbitMQ: Publishing Messages and Queue Listening
Introduction
RabbitMQ is a robust and widely-used message broker that facilitates communication between different parts of a system. In this article, we’ll explore how to integrate RabbitMQ with a .NET Core application. We’ll cover the steps to push messages to a queue and listen for messages, demonstrating a practical use case for asynchronous communication.
Prerequisites
Before we start, make sure you have the following installed:
- .NET Core SDK
- Docker
- A code editor of your choice (e.g., Visual Studio Code, Visual Studio)
Step 1: Setting up RabbitMQ with Docker
To keep things simple, we’ll use Docker to set up a RabbitMQ instance. Run the following command in your terminal:
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
This command pulls the RabbitMQ Docker image and runs it with ports 5672 (AMQP) and 15672 (management UI) exposed.
Step 2: Creating a .NET Core Project
Create a new .NET Core project using the following commands:
dotnet new console -n RabbitMQDemo
cd RabbitMQDemo
dotnet add package RabbitMQ.ClientStep 3: Pushing Messages to RabbitMQ
Let’s create a simple application to push messages to a RabbitMQ queue.
// Program.cs
using System;
using RabbitMQ.Client;
using System.Text;
class Program
{
static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "my_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
string message = "Hello, RabbitMQ!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "", routingKey: "my_queue", basicProperties: null, body: body);
Console.WriteLine($" [x] Sent '{message}'");
}
}
}This code establishes a connection to RabbitMQ, declares a queue named “my_queue,” and sends a message to it.
Step 4: Listening to the Queue
Now, let’s create a consumer to listen for messages from the queue.
// Consumer.cs
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
class Consumer
{
static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "my_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine($" [x] Received '{message}'");
};
channel.BasicConsume(queue: "my_queue", autoAck: true, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}This consumer listens for messages from the “my_queue” and prints them to the console.
Step 5: Run the Application
Run both the producer (publisher) and consumer applications. You should see messages being pushed to the queue and consumed by the listener.
dotnet run -p .\RabbitMQDemo\Program.cs
dotnet run -p .\RabbitMQDemo\Consumer.csConclusion
Congratulations! You’ve successfully set up RabbitMQ with a .NET Core application, pushed messages to a queue, and created a listener to consume those messages. This simple example demonstrates the power of RabbitMQ in facilitating communication between different parts of your system.
Feel free to expand on this foundation by integrating RabbitMQ into your specific use cases, handling more complex message scenarios, and exploring additional RabbitMQ features.
Stackademic
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us on Twitter(X), LinkedIn, and YouTube.
- Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.






