Home > Mobile >  Consumer does not consume messages
Consumer does not consume messages

Time:09-21

This is my mass transit configuration in Program.cs:

    builder.Services.AddMassTransit(x =>
    {
        x.UsingRabbitMq((context,cfg) =>
        {
            cfg.Message<IOrder>(f => f.SetEntityName("new-order"));
            cfg.ReceiveEndpoint("new-order", f => 
            {
                f.Consumer<ConsumeNewOrder>();
            });
        });
    });

   builder.Services.AddHostedService<Worker>();

Background service, which publishes new messages in a new-order queue:

    public class Worker : BackgroundService
    {
        readonly IBus _bus;
        public Worker(IBus bus)
        {
            _bus = bus;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while(!stoppingToken.IsCancellationRequested)
            {
                await _bus.Publish<IOrder>(new { Id = 2 },
                    stoppingToken);
                await Task.Delay(3000, stoppingToken);
            }
        }
    }

Consumer:

public class ConsumeNewOrder : IConsumer<IOrder>
{
    public async Task Consume(ConsumeContext<IOrder> context)
    {
        // do stuff
    }
}

Everything works locally in this project, but if I will split this code in 2 projects (copy IOrder and move consumer), it will not work, because consumer wont receive messages, but there will be new-order_skipped exchange and queue. The _skipped queue appears because consumer does not consume messages, but I don't know why. Please help

CodePudding user response:

There are many of these answers, but I picked the first one.

Messages MUST be the same type, including namespace. It's right at the top in bold in the documentation.

When you split into two projects, your message types must share a namespace.

Also, ensure that all message entity names are configured the same in both the producer and the consumer (your cfg.Message<IOrder>(f => f.SetEntityName("new-order")); method).

  • Related