Adding RabbitMq and MassTransit

microservices csharp aspnetcore

This is the fourth post in the microservices architecture series.

The goal of this post is to use the MassTransit library to publish a message to RabbitMq and have that message consumed by an Api.

Sample Application

Download the sample app from here and walkthrough the changes in the next section.

Source Code Changes

SampleApi1

  • SampleApi1.csproj - Added MassTransit, MassTransit.RabbitMq and AspNetCore.HealthChecks.RabbitMq.

  • SubmitOrderConsumer.cs - The class that consumes the message once it is published on the queue.

  • Program.cs - AddMassTransit is configured. The endpoint order-service is configured and bound to the exchange customer-order.

SampleApi2

  • SampleApi2.csproj - Added MassTransit, MassTransit.RabbitMq and AspNetCore.HealthChecks.RabbitMq.

  • Program.cs - AddMassTransit is configured. A request client is bound to the exchange customer-order.

  • SubmitOrderController.cs - It has a post method that publishes a SubmitOrder request.

Verification

  • F5 should start debugging the docker compose file.

  • Test the SubmitOrderController.

  • It is better to add a breakpoint to the Post method of SubmitOrderController and the Consume method of SubmitOrderConsumer to see the flow.