At First we need to set up the environment for achieving our desired goal. I am using Visual Studio 2019. Hope you already have that. Apart from this you need to install two more things.
- Erlang
- RabbitMQ
RabbitMQ is the most broadly deployed open source message supplier. It supports numerous
messaging protocols. It basically gives our programs a common platform for sending and
receiving messages.
AND
Erlang is a programming language . RabbitMQ server is built on with this language.
1. Setting up the Environment
Install Erlang form here: DOWNLOAD ERLANG INSTALLER
And install RabbitMQ here : DOWNLOAD RABBITMQ INSTALLER
After installing properly browse this link: http://localhost:15672/
This link will open RabbitMQ management login page.
If you are facing problem to bring this page please follow this short blog.
Enabling RabbitMQ Plugin in Windows 10 and 11
Default username & password is guest
2. Getting Started – RabbitMQ with ASP.NET Core
2.1 : First create an asp.net core web application with asp.net core web api. I named my solution as MS1
2.2 : Inside MS1 project install these Nuget packages.
- MassTransit
- MassTransit.RabbitMQ
- MassTransit.AspNetCore
2.3 : Create a folder named ViewModel in the project and add a class named Order.cs. Remove MS1 from ” namespace MS1.ViewModel “
namespace ViewModel { public class Order { public int id { get; set; } public string OrderName { get; set; } public int OrderQty { get; set; } } }
2.4 : Add a controller named OrderController.cs inside Controllers Folder with following code snippets.
using MassTransit; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using ViewModel; namespace MS1.Controllers { [Route("api/[controller]")] [ApiController] public class OrderController : ControllerBase { private readonly IBusControl _bus; public OrderController(IBusControl bus) { _bus = bus; } [HttpPost] public async Task<IActionResult> CreateOrder(Order order) { Uri uri = new Uri("rabbitmq://localhost/event-listener"); var endPoint = await _bus.GetSendEndpoint(uri); await endPoint.Send(order); return Ok("Success"); } } }
2.5 : Go to Startup.cs class and add bellow code at the beginning of ConfigureServices method
services.AddMassTransit(x => { x.UsingRabbitMq(); }); services.AddMassTransitHostedService();
This method should look like this
2.6 : Create another asp.net core web application with asp.net core web api. I named my solution as MS2
2.7 : In order to reduce time, go to MS1 and right click -> edit project file and copy the
following lines and paste to MS-2 by editing project file. Rebuild MS2. This will add the Nuget Packages used in MS1.
<ItemGroup> <PackageReference Include="MassTransit" Version="7.2.4" /> <PackageReference Include="MassTransit.AspNetCore" Version="7.2.4" /> <PackageReference Include="MassTransit.RabbitMQ" Version="7.2.4" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> </ItemGroup>
2.8 : Copy and Paste the ViewModel Folder from the MS1. So that MS1 and MS2 have the same ViewModel.
2.9 : Create a class named EventConsumer.cs and add these lines to consume message
from MS1 via RabbitMQ
using MassTransit; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using ViewModel; namespace MS2 { public class EventConsumer : IConsumer<Order> { public async Task Consume(ConsumeContext<Order> context) { var data = context.Message; } } }
2.10 : Go to Startup.cs class in MS2 and add bellow code at the beginning of ConfigureServices method. And add using GreenPipes; namespace at the top of the MS2 Startup.cs class
services.AddMassTransit(x => { x.AddConsumer<EventConsumer>(); x.UsingRabbitMq((context, cfg) => { cfg.ReceiveEndpoint("event-listener", e => { e.ConfigureConsumer<EventConsumer>(context); }); }); }); services.AddMassTransitHostedService();
This Startup.cs Class should look like this
All Done . Rebuild the Solution .
Before start Right click on the Solution and go to Properties. Select START in Action drop for both project. so that both the projects will run independently. Now run the solution.
3. Testing Phase
3.1 : Give a Debug point on the OrderController.cs and EventConsumer.cs. Two project will run after a successfull launch.
3.2 : Click MS1 POST/api/Order method from Swagger UI. Click Try it out Button. Set id, orderName and orderQty.
{ "id": 1, "orderName": "T-Shirt", "orderQty": 10 }
Check the MS1 Debug Point
Now check the RabbitMQ Dashboard’s Queues Menu. event-listener is remain unchaged.
Press Continue on the VS and this time it will hit the MS2 Debug point. You can check we have the message sending form MS1.
and now reload RabbitMQ Dashboard’s Queues Menu. Here you will see we have one Ready Messages in event-listener.
Continue the solution and remove all debug point and again post data from POST/api/Order
and come back to RabbitMQ Dashboard’s Overview Tab.
In This first image we can see we have 1 Unacked message. Thats mean it is not consumed yet. Now check the image bellow when the message already consumed or deliverd then the Unacked count is 0.
That’s it. Hope this may help you.