Home > Software design >  MassTransit RabbitMq message is not being consumed. C#
MassTransit RabbitMq message is not being consumed. C#

Time:01-31

I have library API in ASP NET Core 3.1 MVC where users can borrow, return and follow borrowed book status. I want to create email notification so when book is returned, all the users that are following this specific book status will recieve email notification that its available.

I want to use RabbitMQ with MassTransit and handle the emails on different web service.

This is my code that is sending messages to the rabbit queue:

public async Task SendNotificationStatus(Book book, CancellationToken cancellationToken)
        {
            var endpoint = await _bus.GetSendEndpoint(new System.Uri($"rabbitmq://{_rabbitHostName}/library-notifications"));
            var bookSpectators = await _userRepository.GetSpectatorsByBookId(book.Id, cancellationToken);

            foreach (var user in bookSpectators)
            {
                NotifyStatusReturn rabbitMessage = new NotifyStatusReturn
                {
                    NotificationType = NotificationTypes.BookReturn,
                    RecipientAddress = user.EmailAddress,
                    RecipientLogin = user.Login,
                    SentDate = DateTime.UtcNow,
                    BookTitle = book.Title
                };
                await endpoint.Send(rabbitMessage);
            }
        }

And when it comes to the notifications service i've created the project with templates provided on MassTransit website - https://masstransit-project.com/usage/templates.html#installation

First I ran dotnet new mtworker -n LibraryNotifications and then after going inside the project folder dotnet new mtconsumer

I've added MassTransit.RabbitMq 8.0.0 package via NugerPackage Manager.

In Contracts folder created via mtconsumer template i ve changed the name of the record to NotifyStatusReturn which look like this:

namespace Contracts
{
    public record NotifyStatusReturn
    {
        public string NotificationType { get; set; }
        public string RecipientAddress { get; set; }
        public string RecipientLogin { get; set; }
        public DateTime SentDate { get; set; }
        public string BookTitle { get; set; }
    }
}

And in Program.cs swapped the x.UsingInMemory() to

x.UsingRabbitMq((context, cfg) =>
{
    cfg.Host("localhost", "/", h =>
    {
        h.Username("guest");
        h.Password("guest");
    });
    cfg.ConfigureEndpoints(context);
});

When i return the book, the message goes into library-notifications_skipped queue as a dead-letter. All the bindings seems okay for me and i really dont know what is the reason that my messages are not being consumed. Could anybody help me with this issue?

CodePudding user response:

As per the documentation:

MassTransit uses the full type name, including the namespace, for message contracts. When creating the same message type in two separate projects, the namespaces must match or the message will not be consumed.

Make sure that your message type has the same namespace/type in each project.

  • Related