jjnrmason
jjnrmason

Reputation: 311

MassTransit Consumer not consuming messages when doing Dependency Injection

I have my consumers and endpoints setup for dependency injection but my consumer isn't getting the messages from the message queue.

I just have 2 messages for the consumer in the queue but mass transit isn't getting them.

This is my main program where I set everything up. The appSettings values are correct.

public class Program {

        public static void Main(string[] args) {
            var executableLocation = Assembly.GetExecutingAssembly().Location;
            Directory.SetCurrentDirectory(Path.GetDirectoryName(executableLocation));

            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .ConfigureServices((hostContext, services) => {
                    var appSettings = new Configuration().GetConfig();
                    var achievementFactory = new AchievementFactory();
                    var client = new MongoClient(appSettings.ConnectionString);
                    var db = client.GetDatabase(appSettings.Database);
                    var mongoRepository = new MongoDBRepository(db);

                    services.AddSingleton(appSettings);
                    services.AddSingleton<IRepository>(mongoRepository);
                    services.AddSingleton<IAchievementFactory>(achievementFactory);

                    services.AddMassTransit(x => {
                        x.AddConsumer<AddAchievementConsumer>();
                        
                        x.UsingRabbitMq((context, cfg) => {

                            cfg.Host(appSettings.QueueUrl, settings => {
                                settings.Username(appSettings.QueueUsername);
                                settings.Password(appSettings.QueuePassword);
                            });

                            cfg.ReceiveEndpoint(appSettings.AchievementQueue, ep =>
                                ep.ConfigureConsumers(context));
                            });
                    });
                });

    }

But my messages are just sitting in the queue and not getting consumed.

Upvotes: 2

Views: 8189

Answers (1)

tgdraugr
tgdraugr

Reputation: 309

Looking to the code, seems to me that you did not start the bus.

Try using services.AddMassTransitHostedService(); after configuring MassTransit

source: https://masstransit-project.com/usage/configuration.html#asp-net-core

Upvotes: 3

Related Questions