AddMasstransitHostedService cannot be found

Viewed 3228

Good day I'm configuring Masstransit for .net6 net core application Have added Masstransit nuget packages:

<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="7.2.4" />
<PackageReference Include="MassTransit.RabbitMQ" Version="7.2.4" />

Am registering it in Startup and it says there is no such method as AddMasstransitHostedService I have tried publishing message without it, but no exchange is being created (and for some reason debug also shows actual address with port 0)

Would be very grateful for help Have searched everything related on the internet, unfortunately no fixes yet

Here is the way I register Masstransit:

services.AddMassTransit(mt =>
{
    mt.UsingRabbitMq((context, cfg) =>
    {
        cfg.Host(new Uri(RabbitMqOptions.RabbitMqUri), h =>
        {
            h.Username(RabbitMqOptions.UserName);
            h.Password(RabbitMqOptions.Password);
        });
        
        cfg.AutoStart = true;

        cfg.Publish<IServerNotificationMessage>(e => e.ExchangeType = RabbitMQ.Client.ExchangeType.Direct);
    });
});
services.AddMassTransitHostedService();//<-----this one hints: IServiceCollection doesnt contain a definition for AddMassTransitHostedService...

and here is the way I've tried publishing message:

public class SomeController : ControllerBase
{
    protected readonly IBus _bus;

    public SomeController(IBus bus)
    {
        _bus = bus;
    }

    [HttpGet("TestPublish")]
    public void TestPublish(CancellationToken cancellationToken)
    {
        _bus.Publish<SomeMessage>(new
        {
            ...
            ... // fields go here
        }, cancellationToken);
2 Answers

You need to add the MassTransit.AspNetCore package reference.

Note that for MassTransit V8, or later, this package is no longer required and should not be referenced.

I see that you added MassTransit v7 but MassTransit v8 has been available since .NET 6 was available. MassTransit v8 integrates a significant portion of the underlying components into a more manageable solution structure. With MassTransit v8, Microsoft.Extensions.Hosting.Abstractions is now included, and the hosted service is registered by default. This means no more separate call to AddMassTransitHostedService and no dependency on MassTransit.AspNetCore.

The hosted service can also be configured using the standard Microsoft configuration extensions. For instance, all of the following options are optional, but can be configured.

builder.Services.AddOptions<MassTransitHostOptions>()
            .Configure(options =>
            {
                // if specified, waits until the bus is started before
                // returning from IHostedService.StartAsync
                // default is false
                options.WaitUntilStarted = true;

                // if specified, limits the wait time when starting the bus
                options.StartTimeout = TimeSpan.FromSeconds(10);

                // if specified, limits the wait time when stopping the bus
                options.StopTimeout = TimeSpan.FromSeconds(30);
            });
Related