How to use RabbitMQ in C#
By FoxLearn 1/7/2025 8:30:20 AM 92
It uses the Advanced Message Queuing Protocol (AMQP) to enable data exchange between processes, applications, and servers. Notable features include extensibility through plugins, support for multiple protocols, high performance, reliability, clustering, and highly available queues. Queues can be created using code, the administration interface, or PowerShell.
In RabbitMQ, two key terms are important:
- Queue: A data structure that follows the FIFO (first in, first out) principle, acting as a buffer to hold data.
- Producer: The component that generates and sends data to the queue.
- Consumer: The component that retrieves and processes data from the queue.
Before installing RabbitMQ, you need to install Erlang.
Download the appropriate version of Erlang for your operating system. Once Erlang is installed, proceed to download and install RabbitMQ server.
After installing Erlang and RabbitMQ on your system, the next step is to set up the RabbitMQ .NET client to interact with the RabbitMQ service.
Programming RabbitMQ in C#
Begin by creating a new console application in Visual Studio. Then, install the RabbitMQ.Client
package using the NuGet Package Manager. If RabbitMQ is running locally on your machine, you can establish a connection to the server with the following code:
ConnectionFactory connectionFactory = new ConnectionFactory(); IConnection connection = connectionFactory.CreateConnection();
If the RabbitMQ service is hosted on a remote server, you can create a method to establish the connection as follows:
public IConnection GetConnection(string hostName, string userName, string password) { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.HostName = hostName; connectionFactory.UserName = userName; connectionFactory.Password = password; return connectionFactory.CreateConnection(); }
Sending and Receiving Messages
With RabbitMQ running locally, you can use this method to send messages to a queue:
public static void Send(string queue, string data) { using (IConnection connection = new ConnectionFactory().CreateConnection()) { using (IModel channel = connection.CreateModel()) { channel.QueueDeclare(queue, false, false, false, null); channel.BasicPublish(string.Empty, queue, null, Encoding.UTF8.GetBytes(data)); } } }
In this method, the connection to RabbitMQ is established using the default configuration. The message sent to the queue is not persistent because the second parameter to QueueDeclare
is set to false
. This means the messages will be stored in memory and will not survive a server restart.
To receive the data from the queue, you can use the following method:
public static void Receive(string queue) { using (IConnection connection = new ConnectionFactory().CreateConnection()) { using (IModel channel = connection.CreateModel()) { channel.QueueDeclare(queue, false, false, false, null); var consumer = new EventingBasicConsumer(channel); BasicGetResult result = channel.BasicGet(queue, true); if (result != null) { string data = Encoding.UTF8.GetString(result.Body); Console.WriteLine(data); } } } }
Running the Send and Receive Methods
Now, you can invoke the Send
and Receive
methods in the Main
function as follows:
static void Main(string[] args) { Send("QueueName", "Hello RabbitMQ!"); Receive("QueueName"); Console.ReadLine(); }
Persistence in RabbitMQ
RabbitMQ offers strong support for message persistence. You can configure two types of queues: Durable and Non-Durable.
- A Durable queue ensures that messages are saved to disk and survive server restarts.
- A Non-Durable queue, on the other hand, stores messages only in memory, which means messages are lost if the server is restarted.
Queue persistence can be set at three levels:
- Queue: Ensures the queue itself survives a restart.
- Exchange: Ensures messages in the exchange are persistent.
- Message: Ensures that each individual message is saved to disk.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#