C# Publish Message to RabbitMQ Queue

In c#, we can publish or consume messages from rabbitmq by using RabbitMQ.Client nuget package. Here, we will learn how to publish a message to rabbitmq using RabbitMQ.Client service in c# or .net application with examples.

C# Create RequestRabbitMQ Application

Let’s create a simple console application with the Name “RequestRabbitMQ” as shown below to publish messages to the rabbitmq queue.

 

C# Create Console Application to Publish Messages to RabbitMQ

 

After creating an application, now we will add the “RabbitMQ.Client” nuget package in our c# application to communicate with the rabbitmq server to publish or consume messages from queues in rabbitmq.

Add RabbitMQ.Client NuGet Package

In c#, we need to add the “RabbitMQ.Client” nuget package in our application to communicate with the RabbitMQ server to publish or consume messages for that, right-click on your application and select Manage NuGet Packages as shown below.

 

C# Add RabbitMQ.Client Nuget Package Reference to Application

 

Now search for RabbitMQ.Client package and install it in your application as shown below.

 

C# Install RabbitMQ.Client Reference in Application

 

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

 

Install RabbitMQ.Client using Package Manager Console

 

Generally, when we publish a message to rabbitmq that will not directly send to the queue, instead first the message will be sent to exchanges, then after that, a routing agent reads and sends it to the appropriate queue with help of header attributes, bindings, and routing keys.

 

So, before we proceed first we need to create an exchangequeue and need to bind a queue to the exchange. We can create exchanges, queues, and bindings either directly from the rabbitmq web management portal or from c# applications using RabbitMQ.Client nuget package service.

 

To know how to create exchangesqueues, and bindings directly from the rabbitmq web management portal check the following articles.

 

In case, if you want to create rabbitmq exchange, queue, and bindings in c#, then we can do it by using RabbitMQ.Client service like as shown below.

C# Create Exchange in RabbitMQ

To create a rabbitmq exchange in c#, first, we need to make a connection with the RabbitMQ server by using RabbitMQ.Client service with required credentials along with hostname.

 

Following is the example of creating an exchange in rabbitmq using c# programming language.

 

using RabbitMQ.Client;
using System;

namespace RequestRabbitMQ
{
   class Program
   {
      static void Main(string[] args)
      {
         string UserName = "guest";
         string Password = "guest";
         string HostName = "localhost";

         //Main entry point to the RabbitMQ .NET AMQP client
         var connectionFactory = new RabbitMQ.Client.ConnectionFactory()
         {
            UserName = UserName,
            Password = Password,
            HostName = HostName
         };

        var connection = connectionFactory.CreateConnection();
        var model = connection.CreateModel();
        Console.WriteLine("Creating Exchange");
        // Create Exchange
        model.ExchangeDeclare("demoExchange", ExchangeType.Direct);
        Console.ReadLine();
      }
   }
}

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 by calling the “CreateConnection” method and creating an exchange by calling the “ExchangeDeclare” method by passing parameters such as “exchange name” and “exchange type”.

 

When we execute the above c# program, we will get the result as shown below.

 

C# Create Exchange in RabbitMQ Example Result

 

After executing the above program, the new exchange (demoExchange) will be created in the rabbitmq server. You can check the newly created exchange (demoExchange) details in the rabbitmq web management portal under the Exchanges tab as shown below.

 

C# RabbitMQ Exchanges Lists in Web Management Portal

 

Now, we will create a queue in rabbitmq using the c# programming language.

C# Create Queue in RabbitMQ

In c#, we can create a queue in rabbitmq by using the “QueueDeclare” method of RabbitMQ.Client service.

 

To create a queue in c#, we need to call the “QueueDeclare” method by passing the required parameters. Following is the definition of the rabbitmq QueueDeclare method in c#.

 

C# QueueDeclare Method in RabbitMQ.Client Service

 

If you observe the QueueDeclare method, it accepts multiple parameters like queue name, durable, exclusive, autodelete, and arguments properties.

 

Following is the sample way of calling the QueueDeclare method to create a queue in rabbitmq using c# programming language.

 

// Create Queue
model.QueueDeclare("demoqueue", true, false, false, null);

C# Create Queue in RabbitMQ Example

Following is the example of creating a queue in rabbitmq using the c# programming language.

 

using RabbitMQ.Client;
using System;

namespace RequestRabbitMQ
{
   class Program
   {
      static void Main(string[] args)
      {
         string UserName = "guest";
         string Password = "guest";
         string HostName = "localhost";

         //Main entry point to the RabbitMQ .NET AMQP client
         var connectionFactory = new RabbitMQ.Client.ConnectionFactory()
         {
            UserName = UserName,
            Password = Password,
            HostName = HostName
         };

         var connection = connectionFactory.CreateConnection();
         var model = connection.CreateModel();

         // Create Queue
         model.QueueDeclare("demoqueue", true, false, false, null);
         Console.WriteLine("Creating Queue");
         Console.ReadLine();
      }
   }
}

If you observe the above example, we are creating a queue (demoqueue) by using the QueueDeclare method in c# programming language.

 

When we execute the above c# program, we will get the result as shown below.

 

C# Create Queue in RabbitMQ Example Result

 

After executing the above program, the new queue (demoqueue) will be created in the rabbitmq server. You can check the newly created queue (demoqueue) details in the rabbitmq web management portal under the Queues tab as shown below.

 

Queues List in RabbitMQ Management Portal

 

After creating a queue and exchange, now we will bind the rabbitmq queue with exchange using c# programming language.

C# Bind RabbitMQ Queue with Exchange

In c#, by using the QueueBind method we can bind a rabbitmq queue with the exchange by passing the name of the exchangequeue along with the routing key name.

 

To bind a queue with an exchange in c#, we need to call the “QueueBind” method by passing the required parameters. Following is the definition of the rabbitmq QueueBind method in c#.

 

C# QueueBind Method in RabbitMQ.Client Service

 

If you observe the QueueBind method, it accepts multiple parameters like a model, queue name, exchange name, and routing key.

 

Following is the sample way of calling the QueueBind method to bind a rabbitmq queue with an exchange in c# programming language.

 

// Bind Queue to Exchange
model.QueueBind("demoqueue", "demoExchange", "directexchange_key");

C# Bind RabbitMQ Queue with Exchange Example

Following is the example of binding a rabbitmq queue with an exchange in c# programming language.

 

using RabbitMQ.Client;
using System;

namespace RequestRabbitMQ
{
   class Program
   {
      static void Main(string[] args)
      {
        string UserName = "guest";
        string Password = "guest";
        string HostName = "localhost";

        //Main entry point to the RabbitMQ .NET AMQP client
        var connectionFactory = new RabbitMQ.Client.ConnectionFactory()
        {
           UserName = UserName,
           Password = Password,
           HostName = HostName
        };

        var connection = connectionFactory.CreateConnection();
        var model = connection.CreateModel();

        // Create Exchange
        //model.ExchangeDeclare("demoExchange", ExchangeType.Direct);
        //Console.WriteLine("Creating Exchange");

        // Create Queue
        //model.QueueDeclare("demoqueue", true, false, false, null);
        //Console.WriteLine("Creating Queue");

        // Bind Queue to Exchange
        model.QueueBind("demoqueue", "demoExchange", "directexchange_key");
        Console.WriteLine("Creating Binding");
        Console.ReadLine();
      }
   }
}

If you observe the above code, we are binding a rabbitmq queue (demoqueue) to exchange (demoExchange) with a routing key (directexchange_key) using the QueueBind method in c#.

 

When we execute the above c# program, we will get the result as shown below.

 

C# Bind RabbitMQ Queue to Exchange Example Result

 

After executing the above program, the defined queue (demoqueue) will be bind to exchange (demoExchange) in the rabbitmq server. You can check the binding details of a queue (demoqueue) in the rabbitmq web management portal under the Bindings panel as shown below.

 

C# Bind Queue to Exchange in RabbitMQ Exchange

 

We are done with the creation of exchange, queue, and binding a queue to exchange in rabbitmq. Now, let’s publish a message to the rabbitmq queue (“demoqueue”) from a console application.

C# Publish Message to RabbitMQ Queue

In c#, by using the BasicPublish method of RabbitMQ.Client service we can publish a messages to particular queue based on our requirements.

 

Following is the definition of the rabbitmq BasicPublish method in c#.

 

C# BasicPublish Method in RabbitMQ.Client Service

 

If you observe the BasicPublish method, it accepts multiple parameters like a model, exchange, routing key, properties, and required message to publish in the rabbitmq queue.

 

Following is the sample way of calling the BasicPublish method to publish a message to the rabbitmq queue in the c# programming language.

 

// Publish message to Queue
model.BasicPublish("demoExchange", "directexchange_key", properties, messagebuffer);

C# Publish Message to RabbitMQ Queue Example

Following is the example of publishing a message to the rabbitmq queue in the c# programming language.

 

using RabbitMQ.Client;
using System;
using System.Text;

namespace RequestRabbitMQ
{
  class Program
  {
    static void Main(string[] args)
    {
      string UserName = "guest";
      string Password = "guest";
      string HostName = "localhost";

      //Main entry point to the RabbitMQ .NET AMQP client var connectionFactory = new RabbitMQ.Client.ConnectionFactory()
      {
        UserName = UserName,
        Password = Password,
        HostName = HostName
      };
      var connection = connectionFactory.CreateConnection();
      var model = connection.CreateModel();
      var properties = model.CreateBasicProperties();
      properties.Persistent = false;
      byte[] messagebuffer = Encoding.Default.GetBytes("Direct Message");
      model.BasicPublish("demoExchange", "directexchange_key", properties, messagebuffer);
      Console.WriteLine("Message Sent");
      Console.ReadLine();
    }
  }
}

If you observe the above example, we are publishing a message (Direct Message) to the queue by passing required parameters like an exchange, routing key, properties, and message to the BasicPublish method.

 

When we execute the above c# program, we will get the result as shown below.

 

C# Publish Message to RabbitMQ Queue Example Result

 

After executing the above program, if we check the queue (demoqueue) details in the rabbitmq web management portal, the Ready column is showing 1 as shown below which means the message was published successfully to demoqueue.

 

C# Queue Publish Message Details in RabbitMQ Web Management