Publish and Consume Messages with MassTransit and RabbitMQ in .Net 6

Ceyhun ÜNLÜ
inventiv
Published in
5 min readMay 3, 2022

In this article, we are going to explain about message broker systems and introduce an open source message bus, called as MassTransit. After the introduction, there will be mini tutorial project with .Net 6.

Message Broker

A message broker is an application that exchanges data between applications, systems, and services, regardless of apps were written in different languages or different environments.

Especially in large systems, when the data flow is very high, the processes must be placed in a certain order because the server capabilities are limited. In large systems, modular and independent applications enable communication with messaging using predetermined standard message. Bottlenecks can occur in communications with “brokerless” architecture. Application communications should not run in a way that blocks each other. Message brokers roles in middle layer can overcome this with queued requests. There are many message brokers which are Azure Service Bus, Apache Kafka and Rabbit Mq are one of the most popular ones.

RabbitMQ

RabbitMQ is a messaging broker for messaging. RabbitMQ enables asynchronous processing. It gives applications a common platform to send and receive messages, and store messages until consumers is ready. RabbitMQ simply stores messages and passes them to consumers when ready.

MassTransit

MassTransit is a lightweight service bus for building distributed .NET applications. The main goal is to provide a consistent, .NET friendly abstraction over the message transport (whether it is RabbitMQ, Azure Service Bus, etc.)

There are a few fundamental concepts we should cover first:

Service Bus: is the term given to the type of application that handles the movement of messages.

Transports are the different types of message brokers MassTransit works with.

Message is a contract, defined code first by creating a .NET class or an interface.

There some of advantages of choosing to use MassTransit instead of working with the native message broker library. First of all, it helps developer to work with multiple message brokers without extra implementation because of abstracting the underlying of message broker logic so that message brokers can be used simultaneously in an environment or different in each environment. In addition to this, MassTransit provides built-in solutions for exception handling, retries, circuit breaker, distributed transaction management, routing, consumer lifecycle management, monitoring and so on. Developers don’t need to implement all of these features. It saves time and helps developer to focus on the needs of their projects.

Now that we have an understanding of what MassTransit is and why we would use it, let’s see how we can use it along with RabbitMQ in .Net 6.

Example scenario is that user wants to notify via calling an api. We are going to develop notifier project which has two apps, a producer api and a consumer console app. Both apps are going to be created with default Visual Studio 2022 templates. Project is going to be using docker compose as orchestrator. Both apps dockerized and rabbitmq runs in docker.

Producer App

Producer app will produce ‘notification-created’ events to RabbitMQ. In order to produce this event, api will provide user to make HTTP request.

We created producer app with default settings of related Visual Studio 2022 template. Now we need to create a message contract to use it during exchange of data between producer and consumer app. Followings will be used in this purpose.

This type will indicate notification message type.
This will be used during data exchange between producer and consumer.

There are different kinds of practices to connect queues to producers and consumers. One of them is to put message contract classes on a class library project and add its reference to related apps because MassTransit includes the namespace for message contracts so that we created the shared library project and set the dependencies.

Now, we are going to set up Program.cs in order to connect app with RabbitMQ via MassTransit

We will configure our RabbitMQ later but in this place we put its name, ‘rabbitmq’. Configurations and credential settings can be configured here. MassTransit provides kinds of configuration. click for more details

Final part for the producer app is the user interaction part. To provide that, we are going to implement notify api with the following:

Our producer app publishes an INotificationCreated event when a user makes a request. In order to do that, MassTransit presents IPublishEndPoint. we can publish our specific event with Publish<T>(obj) method. We can define an anonymous type, ensuring we use the same property names of INotificationCreated.

Consumer App

In order to process INotificationCreated event, we need to implement its consumer. MassTransit provides IConsumer<T> class to consume messages from related queues so that We implemented NotificationCreatedConsumer. INotificationCreated message gets serialized and written in the console.

Now, we are going to set up Program.cs in order to connect app with RabbitMQ via MassTransit.

We registered the consumer by loading executing assembly and giving it to the AddConsumers() method. In order to configure RabbitMQ, UsingRabbitMq method is used.

Docker-Compose

The final part is to configure hosting environment. We are going to run our RabbitMQ, producer and consumer apps on docker so that we need to set up docker-compose.yml

Demo Time

After the configurations and implementation, fun side of these came up. It’s time to test our code. Ensuring we have both the Producer and Consumer applications set up run on startup, running docker compose initiates the environment then go to swagger on a browser and trigger the api https://localhost:7047/swagger/index.html

Api publishes the message and returns success. Let’s see what console looks like:

Our message’s printed in the console.

Conclusion

In this article, we’ve learned about usage of MassTransit with RabbitMQ. MassTransit is powerful message bus library. If message brokers are needed in a .NET project, MassTransit will be useful solution to work with message brokers in terms of ease of use and managing the problems.

You can look at the complete example on GitHub.

Special thanks to Ali Karaca and Evren Pehlivan for great contributions on this medium story.

We also used some references. We are also thanking them :)

Code Maze MassTransit

Thank you for reading :)

--

--