This project provides a guide for setting up WSL 2, installing Docker Desktop, and running RabbitMQ using an official image. Additionally, it includes a practical example of communication with RabbitMQ using two console applications in C#.
-
Install WSL 2 by running the following command in PowerShell:
wsl --install
-
Verify the installation:
wsl -l -v
- Download Docker Desktop from its official page.
- Install and configure Docker Desktop on your system.
Requirements:
- Docker installed and running on your system.
Use the following command to run a RabbitMQ instance with the management plugin enabled:
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:4.0-management
- Error: Permission Denied
If you encounter the error:
error:docker: permission denied while trying to connect to the Docker
, check if thedocker
user has sufficient permissions on the system.
This will start RabbitMQ and expose ports 5672 (messaging) and 15672 (management interface).
Follow the official RabbitMQ tutorial for .NET available at: RabbitMQ Hello World
-
Create a solution with two console projects:
- RabbitMQ-Receive
- RabbitMQ-Send
-
Add the RabbitMQ.Client (7.0.0) dependency to both projects.
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = await factory.CreateConnectionAsync();
using var channel = await connection.CreateChannelAsync();
await channel.QueueDeclareAsync(queue: "hello", durable: false, exclusive: false, autoDelete: false,
arguments: null);
Console.WriteLine(" [*] Waiting for messages.");
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.ReceivedAsync += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine($" [x] Received {message}");
return Task.CompletedTask;
};
await channel.BasicConsumeAsync("hello", autoAck: true, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
using RabbitMQ.Client;
using System.Text;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = await factory.CreateConnectionAsync();
using var channel = await connection.CreateChannelAsync();
await channel.QueueDeclareAsync(queue: "hello", durable: false, exclusive: false, autoDelete: false,
arguments: null);
const string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
await channel.BasicPublishAsync(exchange: string.Empty, routingKey: "hello", body: body);
Console.WriteLine($" [x] Sent {message}");
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
- Start the RabbitMQ-Receive project to listen for messages.
- Run the RabbitMQ-Send project to send a message.
The receiver terminal should display the message:
[x] Received Hello World!