Communication Between ASP.NET Core Applications Using RABBIT MQ

What is RabbitMQ?

RabbitMQ is the most widely deployed open source message broker. It is based on Advanced Message Queuing Protocol (AMQP).  AMQP is merely the protocol that is used to communicate with a message queueing broker like RabbitMQ. If you want to read further about AMQP, click here.

Now, coming back to RabbitMQ, you get a lot of things from RabbitMQ. You can send messages persistently with guaranteed delivery so they will arrive even if your app crashes, and even if the RabbitMQ broker ends up being restarted. You get load balancing between message consumers if you have multiple consumers on the same queue.

RabbitMQ is a bit more than mere messaging, it's a common platform that has the ability to interconnect applications. Using RabbitMQ, a java application can speak to a Linux server and/or a .net app, to a ruby & rails + almost anything that finds its place in the corporate web development.

Why do we need this?

As I have explained above, we can use RabbitMQ to establish communication between different type of technology-based projects.

So by this definition, it is perfect for a microservice architecture. We can make communication through different microservices in our application and by its features, we will be fully assured that the message will be received no matter what.

It is faster than using HttpClient requests because AMQP is a specific protocol whereas HTTP - general-purpose protocol. Thus, HTTP has very high overhead comparing to AMQP.

So now, let’s get started.

To use RabbitMQ you need to download and install a few things on to your machine/server. You can find this information on the web page of RabbitMQ but I’m telling you here step by step what to do.

So first, go to Erlang, download it and then install it. Choose your version correctly. Once you have installed it, the next step is to download and install RabbitMQ server from RabbitMQ website.

Once you are done with that, now we will create our projects. First, we will create a Console ASP.NET Core application namely ConsoleCoreSender.

ASP.NET Core

 

Now, we will create another console app namely ConsoleCoreReceiver. Now, that both our projects are created, we will open the NuGet package manager and add RabbitMq.Client in both our projects.

Right-click on project name and select the option highlighted in the screenshot.

ASP.NET Core

Now, click on the Browse tab, type Rabbit in the search bar and select the option highlighted one in the screenshot.

ASP.NET Core

Now, click on it. Towards the right side, you should find its details with a button called install it.

Click it and let it install.

ASP.NET Core

Do these steps in both the projects. Once you are done, open the sender project and open Program.cs.

In the main method, I have written the following code.

ASP.NET Core

So, let me explain this step by step. The first line is just writing a text in the console.

Now, the next line is -

  1. var factory = new ConnectionFactory() { HostName = "localhost" };  

In the above line, we just created an instance of ConnectionFactory class which will be used to establish a connection to RabbitMQ server that we installed. Here, localhost is because I’m running this application in the local environment. You can pass user id, password and many other parameters as per your settings and requirement. This is basically the server name where your RabbitMQ Server resides. Then, we just created a connection and from that connection, we have created a channel.

We will send our messages through a channel.

  1. channel.QueueDeclare(queue: "msgKey",  
  2.                                      durable: false,  
  3.                                      exclusive: false,  
  4.                                      autoDelete: false,  
  5.                                      arguments: null);  

Now, the above code is important. We have declared a queue and passed it few parameters. The first one is a queue, and that is a secret key and it will be used to send a message and the same key will be used at the receiver end to receive messages and then turned it into bytes array.

In the next line, we have just shown a text to use to enter the message and in the next line, we have just received the user input.

  1. channel.BasicPublish(exchange: "",  
  2.                                      routingKey: "msgKey",  
  3.                                      basicProperties: null,  
  4.                                      body: body);  

In the above code, I have tried to keep it as simple as possible. That’s why I haven't passed exchange or basicProperties. The body is my byte array from user input string and routingKey is our secret key.

For more information about the properties of publishing and create a factory, please click here.

So here is the complete Program.cs class of sender project.

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Console.WriteLine("Hello this is the sender application!");  
  6.   
  7.             var factory = new ConnectionFactory() { HostName = "localhost" };  
  8.             using (var connection = factory.CreateConnection())  
  9.             using (var channel = connection.CreateModel())  
  10.             {  
  11.                 channel.QueueDeclare(queue: "msgKey",  
  12.                                      durable: false,  
  13.                                      exclusive: false,  
  14.                                      autoDelete: false,  
  15.                                      arguments: null);  
  16.   
  17.                 Console.WriteLine("Enter message to send");  
  18.                 var msg = Console.ReadLine();  
  19.                 var body = Encoding.UTF8.GetBytes(msg);  
  20.                 channel.BasicPublish(exchange: "",  
  21.                                      routingKey: "msgKey",  
  22.                                      basicProperties: null,  
  23.                                      body: body);  
  24.                 Console.WriteLine(" [x] Sent {0}", msg);  
  25.             }  
  26.   
  27.             Console.WriteLine(" Press [enter] to exit.");  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  

Now, our work in sender is done. Now, we will go to the receiver project and open its Program.cs.

ASP.NET Core

In this class also, there is just simple queueDelcaration.

  1. channel.QueueDeclare(queue: "msgKey",  
  2.                                      durable: false,  
  3.                                      exclusive: false,  
  4.                                      autoDelete: false,  
  5.                                      arguments: null);  

As you can see I have kept this declaration to as simple as possible because the motive of this exercise to simply send a message and receive at the other end. You can customize it in the future. I’m just showing you the extreme basics.

Here again, our queue is the secret key that is used by the sender to send messages.

  1. var consumer = new EventingBasicConsumer(channel);  
  2.                 consumer.Received += (model, ea) =>  
  3.                 {  
  4.                     var body = ea.Body;  
  5.                     var message = Encoding.UTF8.GetString(body);  
  6.                     Console.WriteLine(" [x] Received {0}", message);  
  7.                 };  
  8.                 channel.BasicConsume(queue: "msgKey",  
  9.                                      autoAck: true,  
  10.                                      consumer: consumer);  

In the above code, I have just made an event so that every time a sender sends something, it automatically receives it and displays it in the console.

So, that’s it for our receiver class. Here is our entire class.

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Console.WriteLine("Hello this is the Receiver application!");  
  6.   
  7.             var factory = new ConnectionFactory() { HostName = "localhost" };  
  8.             using (var connection = factory.CreateConnection())  
  9.             using (var channel = connection.CreateModel())  
  10.             {  
  11.                 channel.QueueDeclare(queue: "msgKey",  
  12.                                      durable: false,  
  13.                                      exclusive: false,  
  14.                                      autoDelete: false,  
  15.                                      arguments: null);  
  16.   
  17.                 var consumer = new EventingBasicConsumer(channel);  
  18.                 consumer.Received += (model, ea) =>  
  19.                 {  
  20.                     var body = ea.Body;  
  21.                     var message = Encoding.UTF8.GetString(body);  
  22.                     Console.WriteLine(" [x] Received {0}", message);  
  23.                 };  
  24.                 channel.BasicConsume(queue: "msgKey",  
  25.                                      autoAck: true,  
  26.                                      consumer: consumer);  
  27.   
  28.                 Console.WriteLine(" Press [enter] to exit.");  
  29.                 Console.ReadLine();  
  30.             }  
  31.         }  
  32.     }  

Now, let's run and test these two applications.

ASP.NET Core

 

As you can see I have run both the solutions. Now, just type and hit enter in sender application and it should reflect in receiver application.

ASP.NET Core

If you wish to see/download the code click here for the sender and here for the receiver.

Summary

In today’s article, we have seen how we can communicate between two different ASP.NET Core 2.0 console applications using RabbitMQ.

Hope you all liked it.

Happy Coding!