Featured

Microservice Communication with MassTransit and RabbitMQ with ASP.NET Core/6

Last modified: May 29, 2022

What is RabbitMQ?

Rabbit is one of the most popular open source Message Broker. It allow to receive and send message from publisher to consumer , where data never lost even the consumer application is offline temporary. A message can be simple as a string to complex nested class. It also has a dashbaord to overview the message transport. Some of the advantage of using RabbitMQ are better scalability, clean User Experience and Higher availability.

What is MassTransit?

MassTransit is also open source application framework to that helps .NET developers route messages over RabbitMQ. Some of the benefits are

  • communcation higher availability,
  • reliabiity and
  • scalability

It support In Memory, RabbitMQ, Azure Service Bus, SQS

Installing RabbitMQ as a Service in Windows

Before installing RabbitMQ, we need to install programming language ErLang

ErLang

I am using Windows (10) PC.Download it from https://www.erlang.org/downloads.Once the installation is completed, now are going to RabbitMQ as a Service in Windows

RabbitMQ

project Download it from https://www.rabbitmq.com/install-windows.html

project

Open the Command prompt in Window PC. Run the following line by each line at a time

cd C:\Program Files\RabbitMQ Server\rabbitmq_server-3.10.2\sbin rabbitmq-plugins enable rabbitmq_management

project project

ASP.NET 6 Microservices

We are going to make two Microservices

  • Product Microservice
  • Shopping Cart Microservice

Before creating these two microservice, we need to create a Shared Project containing models of both of mircoservice as RabbitMQ require same namespace to work.

1. Create a blank solution

project project

  • Create a folder named Microservices and Shared folder

project

Shared Project

  • Create a Shared library (aka Class Libary)
    • Delete Class1.cs
    • Create a Class Product
    namespace Shared { public class Product { public int Id { get; set; } public string Sku { get; set; } public string Name { get; set; } public double Price { get; set; } public string Brand { get; set; } } }

project

Setting up the Publisher

  • Create WebAPI project - Product Microservice (Publisher)
    • Delete old codes (WeatherForecastController.cs & WeatherForecast.cs)
    • Install Nuget Packages
    <PackageReference Include="MassTransit" Version="8.0.2" /> <PackageReference Include="MassTransit.RabbitMQ" Version="8.0.2" />
    • Configuring MassTrasit in Program.cs
    builder.Services.AddMassTransit(x => { x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(config => { config.Host(new Uri("rabbitmq://localhost"), h => { h.Username("guest"); h.Password("guest"); }); })); }); builder.Services.AddMassTransitHostedService(); builder.Services.AddControllers();

project project

  • Create a controller called ProductController to pass value from product microservice to shoppingcart microservice which endpoint cart
namespace ProductMicroservice.Controllers { [Route("api/[controller]")] public class ProductController : ControllerBase { private readonly IBus _bus; public ProductController(IBus bus) { _bus = bus; } [HttpPost] public async Task<IActionResult> CreateCart(Product product) { if(product != null) { Uri uri = new Uri("rabbitmq://localhost/cart"); var endPoint = await _bus.GetSendEndpoint(uri); await endPoint.Send(product); return Ok(); } return BadRequest(); } } }

I used Swagger UI to execute the code project

And we see it is on queue on RabbitMQ, since we do not have a consumer (i.e. Shopping Cart Microservice) project

Setting up the Consumer

  • Create WebAPI project - Shopping Cart Microservice (Consumer)
    • Delete old codes (WeatherForecastController.cs & WeatherForecast.cs)
    • Install Nuget Packages
    <PackageReference Include="MassTransit" Version="8.0.2" /> <PackageReference Include="MassTransit.RabbitMQ" Version="8.0.2" />

project project

  • Configuring MassTrasit in Program.cs
builder.Services.AddMassTransit(x => { x.AddConsumer<ShoppingCartConsumer>(); x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg => { cfg.Host(new Uri("rabbitmq://localhost"), h => { h.Username("guest"); h.Password("guest"); }); cfg.ReceiveEndpoint("cart", ep => { ep.PrefetchCount = 16; ep.UseMessageRetry(r => r.Interval(2, 100)); ep.ConfigureConsumer<ShoppingCartConsumer>(provider); }); })); }); builder.Services.AddMassTransitHostedService(); builder.Services.AddControllers();
  • Create a consumer called ShoppingCartConsumer
public async Task Consume(ConsumeContext<Product> context) { var data = context.Message; }

We can see message in consumer project