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 Name “RequestRabbitMQ” like as shown below to publish a messages to rabbitmq queue.

 

C# Create Console Application to Publish Messages to RabbitMQ

 

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

Add RabbitMQ.Client NuGet Package

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

 

C# Add RabbitMQ.Client Nuget Package Reference to Application

 

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

 

C# Install RabbitMQ.Client Reference in Application

 

Following is the another way to install RabbitMQ.Client package by using Package Manager Console like as shown below by entering 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 queue, instead first the message will be send 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 exchangequeue and need to bind queue to exchange. We can create exchanges, queues and bindings either directly from rabbitmq web management portal or from c# applications using RabbitMQ.Client nuget package service.

 

To know how to create exchangesqueues and bindings directly from rabbitmq web management portal check 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 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 above example, to establish a connection with rabbitmq server we are passing a required credentials along with HostName to ConnectionFactory() method. After that, we created a connection by calling “CreateConnection” method and creating an exchange by calling “ExchangeDeclare” method by passing a parameters such as “exchange name” and “exchange type”.

 

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

 

C# Create Exchange in RabbitMQ Example Result

 

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

 

C# RabbitMQ Exchanges Lists in Web Management Portal

 

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

C# Create Queue in RabbitMQ

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

 

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

 

C# QueueDeclare Method in RabbitMQ.Client Service

 

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

 

Following is the sample way of calling 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 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 above example, we are creating a queue (demoqueue) by using QueueDeclare method in c# programming language.

 

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

 

C# Create Queue in RabbitMQ Example Result

 

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

 

Queues List in RabbitMQ Management Portal

 

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

C# Bind RabbitMQ Queue with Exchange

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

 

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

 

C# QueueBind Method in RabbitMQ.Client Service

 

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

 

Following is the sample way of calling QueueBind method to bind a rabbitmq queue with 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 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 above code, we are binding a rabbitmq queue (demoqueue) to exchange (demoExchange) with routing key (directexchange_key) using QueueBind method in c#.

 

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

 

C# Bind RabbitMQ Queue to Exchange Example Result

 

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

 

C# Bind Queue to Exchange in RabbitMQ Exchange

 

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

C# Publish Message to RabbitMQ Queue

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

 

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

 

C# BasicPublish Method in RabbitMQ.Client Service

 

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

 

Following is the sample way of calling BasicPublish method to publish a message to rabbitmq queue in 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 rabbitmq queue in 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 above example, we are publishing a message (Direct Message) to queue by passing required parameters like exchange, routing key, properties and message to BasicPublish method.

 

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

 

C# Publish Message to RabbitMQ Queue Example Result

 

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

 

C# Queue Publish Message Details in RabbitMQ Web Management