A functioning installation of the dotnet runtime and sdk (at least 6.0).
A high performance transport that allows both cloud based and local development.
Add the MassTransit and MassTransit.RabbitMQ packages to the project.
$ dotnet add package MassTransit
$ dotnet add package MassTransit.RabbitMQ
MassTransit includes project and item templates simplifying the creation of new projects. Install the templates by executing dotnet new -i MassTransit.Templates at the console.
$ dotnet new --install MassTransit.Templates
To create a service using MassTransit, create a worker via the Command Prompt.
$ dotnet new mtworker -n MassTransitSample
$ cd MassTransitMassSample
$ dotnet new mtconsumer
When you open the project you will see that you have 3 class files.
Program.cs
is the standard entry point and here we configure the host builder.Consumers/GettingStartedConsumer.cs
is the MassTransit ConsumerContracts/MassTransitSample.cs
is an example messag
In the root of the project add Worker.cs
using MassTransit;
namespace MassTransitSample
{
public class Worker : BackgroundService
{
readonly IBus _bus;
public Worker(IBus bus)
{
_bus = bus;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await _bus.Publish(new Contracts.MassTransitSample { Value = $"The time is {DateTimeOffset.Now}" }, stoppingToken);
await Task.Delay(1000, stoppingToken);
}
}
}
}
In Program.cs
at CreateHostBuilder
method.
using MassTransit;
using MassTransitSample;
using Company.Consumers;
namespace MassTransitSample
{
public class Program
{
public static async Task Main(string[] args)
{
await CreateHostBuilder(args).Build().RunAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddMassTransit(x =>
{
x.AddConsumer<MassTransitSampleConsumer>();
x.UsingRabbitMq((context, cfg) =>
{
cfg.Host("localhost", "/", h =>
{
h.Username("admin");
h.Password("admin");
});
cfg.ConfigureEndpoints(context);
});
});
services.AddHostedService<Worker>();
});
}
}
localhost
is where the docker image is running. We are inferring the default port of 5672
and are using \
as the virtual host (opens new window). guest
and guest
are the default username and password to talk to the cluster and management dashboard (opens new window).
$ dotnet run