RabbitMQ with ASP.NET Core — Microservice Communication with MassTransit

In this article, we will see Microservice Communication using RabbitMQ with ASP.NET Core. We will learn how to enable communication between Microservices using RabbitMQ and MassTransit.

We will be using MassTransit Helpers to publish / receive messages from our RabbitMQ server

What is Message Broker

Before going to topic RabbitMQ, we will see about Message Broker. Message Broker main responsibility is to broker messages between publisher and subscribers.

Once a message is received by a message broker from producer, its routes the message to a subscriber. Message broker pattern is one of the most useful pattern when it comes to decoupling microservices.

Producer: An application responsible for sending message.

Consumer: An application responsible for messages.

Queue: Storage where messages are stored by the

What is RabbitMQ?

RabbitMQ is one of most widely used open-source message-Broker service. It basically gives your applications a common platform for sending and receiving messages. This ensures that our messages (data) is never lost and successfully receives to consumers and it supports various messaging protocols.

Advantages Of RabbitMQ

There are a some reasons why using a queue instead of directly sending data is better:

  • Higher availability and better error handling

Demonstration of RabbitMQ setup

Simple demonstration of RabbitMQ setup. In case, any of consumer is offline for sometime, messages are still in RabbitMQ waiting for consumers to come online and receives messages.

Protocol supported

RabbitMQ supports multiple protocols.

  • AMQP 0–9–1: RabbitMQ was originally developed to AMQP 0–9–1. AMQP 0–9–1 is a binary protocol, and defines quite strong messaging semantics

What is MassTransit

MassTransit essentially helps developers to route messages over Messaging Service Buses, with support for RabbitMQ. MassTransit does not have a specific implementation. It basically works like an interface, an abstraction over the whole message bus concept

Setting Up The Environment

For installing RabbitMQ, there is multiple approach, I would recommend for Approach 2 i.e., by installing via docker images

Approach 1

Install ErLang and RabbitMQ in your local machine

Approach 2

Or run docker rabbitMQ with management web console

docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management

To Enable RabbitMQ managementt plugin- Dashboard

To activate RabbitMQ management dashboard, run below command in command prompt with administrator

cd C:\Program Files\RabbitMQ Server\rabbitmq_server-3.8.17\sbin
rabbitmq-plugins enable rabbitmq_management
net stop RabbitMQ
net start RabbitMQ

Now navigate to http://localhost:15672, where you can find management dashboard of RabbitMQ running

The default user id and password for management is guest/guest.

Getting Started

Nuget library used in application

Install-Package MassTransit
Install-Package MassTransit.AspNetCore
Install-Package MassTransit.RabbitMQ

Clone Source code and you can see project structure via solution explorer. We walk through code


This is common class library to all the application. Here we have Model (i.e., Todo class) and Constant (For RabbitMQ configuration)

Below is code snippet for Todo and Constant class


Publisher class library

We will configure MassTransit in startup.cs file in our Asp.net core Web api application.


And in our publisher controller, will create post method to publish message


Notification class library

For testing, we will see console application. In below code snippet will register for consuming in Program.cs file and will write consume method in TodoConsumerNotification.cs file to display messages


Testing our application

Scenario 1: Keeping Publisher and Consumer/Notification running as Multiple startup projects

Multiple startup projects

Call post method api/todo via Swagger with below


Scenario 2: Keeping Publisher set as start up project and Consumer/Notification running as offline

Call post method api/todo via Swagger with below

You can see that our Queue has 1 new Message Ready which is not deliver yet. It will keep the message in memory till a consumer is connected.

Let’s bring our Consumer online now, and we can see message is delivered and Queue is 0


In this article, we have gone through Message Brokers, RabbitMQ, Advantages , Integrating RabbitMQ with ASP.NET Core using MassTransit. We also build a small prototype application to send data over the RabbitMQ Server. Download source code from GitHub

Technical lead with over 10+ years of work experience in Analysis, Design and Development of Software Applications Architecture https://github.com/Amitpnk