![]() We’ll need to add the MediatR and MassTransit packages next: We can remove WeatherForecast.cs and WeatherForecastController as we won’t need these. ![]() Let’s start with creating the initial service using the ASP.NET Web API template in Visual Studio or run the below command if you’re using VSCode or other editor:ĭotnet new webapi -name MassTransitMediatR When the message is received, we need to call a Car Booking API through the ICarBookingService. In our sample app, we have a Car Booking Service and will be receiving a BookingCreated message from another upstream service. Getting started with MassTransit and MediatR So, if we introduce MediatR, you could receive the message using MassTransit’s consumer which then sends out requests or notifications to multiple MediatR handlers so you can simplify the solution and follow the Single Responsibility Principle to make your code more maintainable. When receiving a message, you may want to perform many actions on this message including validation and logging but you could be writing to a database then calling an API before sending out other messages (after some other transformations). You can find out more about MediatR here. MediatR also adds pipeline behaviours so you can add validators or loggers into the request pipeline so this helps keep that code out of your handlers where you may have business logic. If you need to add additional handlers, you no longer need to add additional services in your IoC container or inject them into your controller. With MediatR, you can simply inject an IMediator and this will send out requests or notifications which are automatically routed to the right handlers for you. For example, if you have an API with a controller that needs to call out to a few services, each of these services needs to be injected into the constructor and registered in your IoC container. The Mediator pattern is a way to decouple code which is great for complex codebases. MediatR is another Open Source library used to implement the Mediator pattern. To see how this can work with ASP.NET APIs, see ASP.NET Correlation ID. With MassTransit, you can also configure your CorrelationId and MessageIds so that you can trace your requests through your distributed system. You can find out more about MassTransit here. There’s also lots of more advanced features such as sagas, configuration of retries, exponential back-off and a test harness to simplify integration testing etc. NET which greatly simplifies routing messages to handlers (known as consumers) and works with many message brokers such as Azure Service Bus, RabbitMQ. MassTransit is a highly popular Open Source messaging library for. We’ll start out with creating the MassTransit messages and consumers then move on to creating the MediatR requests and handlers and then glue it all together using our IoC container. In this post we’ll show how to get started with MassTransit and MediatR in an ASP.NET 6 application.
0 Comments
Leave a Reply. |