I am building a microservice-based application that contains two microservices which are communicating over a RabbitMQ broker. An event is published whenever a book is created, updated or deleted. Publishing from the first microservice is working fine, but nothing is consumed in the other microservice. Nothing is put into the queues although the queues are up and running. I am using MassTransit and RabbitMQ with a .Net application using Visual Studio 2019. The following is my configurations code in Startup.cs
services.AddMassTransit(x =>
{
x.AddConsumer<BookCreationConsumer>();
x.AddConsumer<BookUpdatingConsumer>();
x.AddConsumer<BookDeletionConsumer>();
x.UsingRabbitMq((context, configurator) =>
{
var rabbitMQSettings = Configuration.GetSection(nameof(RabbitMQSettings)).Get<RabbitMQSettings>();
configurator.Host(rabbitMQSettings.Host);
configurator.ReceiveEndpoint("BookCreation-Queue", c =>
{
c.ConfigureConsumer<BookCreationConsumer>(context);
});
configurator.ReceiveEndpoint("BookUpdating-Queue", c =>
{
c.ConfigureConsumer<BookUpdatingConsumer>(context);
});
configurator.ReceiveEndpoint("BookDeletion-Queue", c =>
{
c.ConfigureConsumer<BookDeletionConsumer>(context);
});
});
});
services.AddMassTransitHostedService();
I Implemented the consumer classes to inherit from IConsumer MassTransit interface. Even though, the receiver microservice is not consuming messages. Consume method is not hit at all! The following is the code for the creation event consumer:
public class BookCreationConsumer : IConsumer<BookCreationEvent>
{
private readonly IBooksInfoRepository repository;
public BookCreationConsumer(IBooksInfoRepository repository)
{
this.repository = repository;
}
public async Task Consume(ConsumeContext<BookCreationEvent> context)
{
var message = context.Message;
var book = await repository.GetBookInfo(message.BookID);
if (book != null)
return;
book = new BookInfo
{
id = message.BookID,
Title = message.Title,
Author = message.Author,
Edition = message.Edition,
NumberOfPages = message.NumOfPages,
BookURL = message.BookURL
};
await repository.AddBookInfo(book);
}
What could be the cause for the non-consuming of messages?
Thanks
Check the receiving endpoint queue and exchange bindings. Most probably, the consumer has massage contracts declared in a different namespace. You will also see that not only your consumer is not triggered, but also the queues of your endpoints are empty.
The endpoint queue must be bound to an endpoint exchange, and the endpoint exchange must be bound to the message exchange. Each message exchange has the name of the message contract, which is published there. If the namespace of the published message contract doesn't match the namespace of the message contract on the consumer side, the bindings won't be created correctly.
It's fully described in the docs.
It's all can be easily checked in the RabbitMQ admin web interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With