Home > Software engineering >  AddMasstransitHostedService cannot be found
AddMasstransitHostedService cannot be found

Time:12-02

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);

CodePudding user response:

You need to add the MassTransit.AspNetCore package reference.

  • Related