This article shows you how to use Rabbit MQ with c# application. Here I will explain how to consume the WEB API, how Rabbit MQ is broadcasting, and how the client application uses the broadcast message.
What is Rabbit MQ & its features?
RabbitMQ is an open-source message broker software that facilitates communication and data exchange between various components of distributed applications. RabbitMQ functions as a message queue, managing the routing, queuing, and delivery of messages from senders (producers) to receivers (consumers).
- Decoupling applications: RabbitMQ can be used to decouple applications, which means that they can communicate with each other without being a monolith.
- Implementing async communication: RabbitMQ can be used to implement asynchronous communication, which means that messages are sent and received without blocking the sender or receiver.
- Real-time streaming: RabbitMQ can be used to implement real-time streaming, which means that messages are delivered in real time.
- Load balancing: RabbitMQ can be used to load balance messages between different consumers.
- Failover: RabbitMQ can be used to provide failover for applications. This means that if one RabbitMQ node fails, another node can take over and continue to deliver messages.
- Auditing: RabbitMQ can be used to audit messages. This means that you can track who sent and received messages and when they were sent and received.
- Monitoring: RabbitMQ can be monitored to track its performance and health.
Benefits of RabbitMQ
- Multi-platform communication which means that messages are serialized/deserialized in common languages such as JSON.
- Open source for that very large community can develop and improve the issues.
- Flexible, Reliable, and Scalable.
- Support Multiple protocols (STOMP, MQTT, RPC, HTTPS, HTTP, etc).
RabbitMQ Installation
Download Rabbit MQ for Windows as I am using Windows Operating System.
RabbitMQ Download Link and Installation Process
Default Configuration
- Endpoint address: http://localhost:15672/
- Login: guest
- Password: guest
- Windows Service Name: RabbitMQ
- Windows menu item to start the service if not started: RabbitMQ Service - start
Create Dotnet core webapi and broadcast the message (Using the Latest .Net 8).
- Tools: Visual Studio 2022
- Check the latest dotnet version
- Open CMD and type: dotnet --version
- Check all the versions: dotnet --list-SDKs
Step 1. Add new dotnet webapi project.
![Dotnet webapi]()
Step 2. Select the framework.
![Framework]()
Step 3. Create the project and also please check and confirm that the project is the .net 8 framework.
![.Net 8 framework]()
Then start the coding
Add NewFolder call Model, and add Product Model class with the name of Product.cs.
Add NewFolder call RabbitMQ, add Rabbitmq interface name with IRabbitMQProducer.cs.
Add a new RabbitMQProducer Class and consume the IRabbitMQProducer interface. Please install the latest RabbitMQ.Client from nuget.org.
Add a New folder called Data ( Which is used for Database purposes), Please be sure that you have installed Entity framework for this project using nuget.org.
Add NewFolder Services and add Service Interface name with IProductService.cs.
Add ProductService.cs class use IProductServices.
Add-Migration script, execute the below command in the Package Manager console.
Then update the DB by passing the below command.
Now add NewFolder, named with controllers, and add ProductController.cs.
Update the Program.cs class with code below.
Now build the project and run it on a local machine.
![Local machine]()
Add item into product DB.
![Product DB]()
Please check the DB.
![DB]()
Done
Will see in the next Part, How to consume the message in the client application.