MassTransit - What,Why,How
MassTransit is a powerful open-source library tailored for the .NET platform, which serves as a messaging and microservice orchestration framework. Adopting MassTransit for building applications in C# comes with many benefits and is instrumental under various scenarios.
Let's delve into the key reasons and benefits of using MassTransit, and explore some examples of when it should be employed.
Examples of When to Use MassTransit:
MassTransit is an amazing tool for the modern C# developer looking to build robust, scalable, and maintainable applications. Whether you're orchestrating a fleet of microservices or simply need a reliable way to communicate between distributed application components, MassTransit represents a strong choice that merits consideration, of course, you have more alternatives such as Wolverine, however, won't cover this one in this article.
Let's Start to work with MassTransit
You can download the demo project at https://github.com/ljscodex/NET8_MassTransitDemo
But if you want to implement MassTransit in your project, let me show you the basic steps:
You need to download the MassTransit Package from Nuget
Now, you need to let your App knows you want to use Masstransit so, let's go to the Program.CS file and add the following Line:
builder.Services.AddMassTransit( x=>
{
x.AddConsumers(typeof(Program).Assembly);
x.UsingInMemory (( context, cfg) =>
{
cfg.ConfigureEndpoints(context);
});
});
Here we are telling the App we want to use MassTransit with InMemory Transport. here is where we have to change to UsingRabbitMQ, AmazonSQS, or AzureSB.
In this case, I used
x.AddConsumers(typeof(Program).Assembly);
it because I want to use all the Consumers on my App.
领英推荐
Now, we need to create a Contract to let the publisher and consumer know, which will be the Message Format.
In my case, I created a Person Record inside a MassTransitContract class
public class MasstransitContracts
{
public record Person ()
{
public string Name { get; init; }
public int Age {get; init;}
};
}
After the Contract, we need to create a consumer, so let's create a Consumer Folder and add a new File (MasstransitDemoConsumer.cs)
namespace NET8_MassTransit_Demo.Consumers;
using System.Threading.Tasks;
using NET8_MassTransit_Demo.Contracts;
using MassTransit;
using Microsoft.Extensions.Logging;
using static NET8_MassTransit_Demo.Contracts.MasstransitContracts;
public class GettingStartedConsumer :
IConsumer<Person>
{
readonly ILogger<GettingStartedConsumer> _logger;
public GettingStartedConsumer(ILogger<GettingStartedConsumer> logger)
{
_logger = logger;
}
public Task Consume(ConsumeContext<Person> context)
{
_logger.LogInformation($"Received Person: {context.Message.ToString()}");
return Task.CompletedTask;
}
}
and now, we just need to start to use it.
In your Services/Repository add the IBus interface
private readonly IBus _masstransitBus;
public MassTransitDemoController( IBus masstransitBus)
{
_masstransitBus = masstransitBus;
}
YES, I used the controller (demo proposes) :P, but the idea is the same.
Now we can start to send messages from our methods, in my case I use the Get method in the controller to create the publisher.
[HttpGet]
public ActionResult<Person> Get([FromQuery]Person p)
{
_masstransitBus.Publish(p);
return p;
}
Here is the magic, now, every time somebody consumes the Get endpoint, the consumers will receive a message with the Person information.
I hope this article was useful and if you have any trouble using it, just let me know!.