Home > Software design >  When I Inject any type of DI inside my Consumer, it returns this Error in my Startup (MassTransit)
When I Inject any type of DI inside my Consumer, it returns this Error in my Startup (MassTransit)

Time:09-16

In my project, I use MassTransit to publish and subscribe to Events and also I use RabbitMQ as a message broker.

I have a simple consumer here. Without DI (private readonly ILogger<ConsultantFinishedEventConsumer> logger) Everything works and all consumers catch the event fine, but when I Inject any type of DI inside my Consumer, It returns this Error in my Startup.CS (In Compile-time):

Severity Code Description Project File Line Suppression State Error CS0310 'ConsultantFinishedEventConsumer' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TConsumer' in the generic type or method 'ConsumerExtensions.Consumer(IReceiveEndpointConfigurator, Action<IConsumerConfigurator>)' Consultant.API ...\Infrastructure\DependencyInjection.cs 96 Active

This is my Consumer

 public class ConsultantFinishedEventConsumer : IConsumer<ConsultantFinishedEvent>
    {
        private readonly ILogger<ConsultantFinishedEventConsumer> logger;
        public ConsultantFinishedEventConsumer(ILogger<ConsultantFinishedEventConsumer> logger)
        {
            this.logger = logger;
        }
        public Task Consume(ConsumeContext<ConsultantFinishedEvent> context)
        {
            return null;
        }
    }

Ans here is my startUp config:

#region ServiceBus adding
    public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddMassTransit(x =>
        {
            x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(config =>
            {
                config.Host("RabbitURL", h =>
                {
                    h.Username("Username");
                    h.Password("Pass");
                });
                //Register Consumers
                config.ReceiveEndpoint("ConsultantQueue", ep =>
                {
                    ep.PrefetchCount = 16;
                    //vvvvvvvvvvvv Here I got compile time Err  
                    ep.Consumer<ConsultantFinishedEventConsumer>();;
                });
            }));
            //Register RequestClients
            RegisterClients(x);

        });
        services.AddMassTransitHostedService();

        return services;
    }

 
    #endregion

CodePudding user response:

Since this question was posted just before your follow up question, I'm going to use the code from that question to explain what is wrong with the code above.

The proper configuration should use the ConfigureConsumer method as shown below.

public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
{
    services.AddMassTransit(x =>
    {
        RegisterMessageConsumers(x);
        RegisterRequestClients(x);

        x.UsingRabbitMq((context, cfg) =>
        {
            cfg.Host(configuration["EventBusConnection"], h =>
            {
                h.Username(configuration["EventBusUserName"]);
                h.Password(configuration["EventBusPassword"]);
            });

            cfg.ReceiveEndpoint("ConsultantQueue", ep =>
            {
                ep.PrefetchCount = 16;

                ep.ConfigureConsumer<ConsultantFinishedEventConsumer>(context);
            });
            
            cfg.ConfigureEndpoints(context);
        });
    });
}
  • Related