Home > Back-end >  Is IHostedService for start/stop IBus needed in MassTransit V8
Is IHostedService for start/stop IBus needed in MassTransit V8

Time:03-30

I am using MassTransit with RabbitMQ in a ASP.Net Core (.Net 6) microservices project. Now I have upgraded to MassTransit Version 8. Currently I am using a IHostedService to start and stop the Bus:

public class BusService : IHostedService
{
    private readonly IBusControl _busCtrl;

    public BusService( IBusControl busCtrl )
    {
        _busCtrl = busCtrl ?? throw new ArgumentNullException( nameof( busCtrl ) );
    }

    public Task StartAsync( CancellationToken cancellationToken )
        => _busCtrl.StartAsync( cancellationToken );

    public Task StopAsync( CancellationToken cancellationToken )
        => _busCtrl.StopAsync( cancellationToken );
}

Service Registration:

services.AddSingleton<IHostedService, BusService>();

I read now that "MassTransit will automatically add an IHostedService for MassTransit". So does this mean that I don't need such a Service to start/stop the bus anymore?

CodePudding user response:

Correct, you no longer need to add your own hosted service. Calling AddMassTransit will add all required services and health checks to the service collection.

The above service would be duplicative, and should not be used with MassTransit v8.

  • Related