Home > Net >  c# Rebus - Message not being sent to RabbitMQ
c# Rebus - Message not being sent to RabbitMQ

Time:05-28

I'm trying to publish an event from one API and subscribe to this event from multiple other API's. I'm trying to accomplish this using Rebus, RabbitMQ and .NET 5.

No matter what I do, the message does not seem to be sent to the queue at all.

my code looks like this

Sending API

Startup.cs - ConfigureServices

        services.AddRebus(configure => configure
            .Logging(x => x.Serilog())
            .Transport(x => x.UseRabbitMq("amqp://guest:guest@localhost:5672", "Bus.Users"))
            .Routing(x => x.TypeBased()
                .Map<UserUpdatedEvent>("Bus.Users")));

Startup.cs - Configure

            app.ApplicationServices.UseRebus();

Code that triggers the publish:

                var userUpdatedEvent = new UserUpdatedEvent
            {
                Id = user.Id,
                FirstName = user.FirstName,
                LastName = user.LastName,
                ProfileImageId = user.ProfileImageUuid,
                ProfileImageUrl = user.ProfileImageUrl
            };

            await _bus.Publish(userUpdatedEvent);

This code gets triggered everytime, though I never see a message in the queue or error queue in rabbitMQ management.

I've been trying to fix this issue for quite some time now but can't seem to find any solution. Does anybody have any idea what I'm doing wrong?

Kind regards

CodePudding user response:

This part

.Routing(x => x.TypeBased()
                .Map<UserUpdatedEvent>("Bus.Users"))

tells Rebus that you want messages of type UserUpdatedEvent to be routed to the queue "Bus.Users", so that when you

await bus.Send(new UserUpdatedEvent(...));

the message will be put into the queue "Bus.Users".

It's a simple point-to-point channel, where a sender sends a message directly to the recipient's input queue.

That's probably not what you want in this case, since the message is an event. You usually use point-to-point channels for commands, e.g. UpdateUserCommand instructing the recipient to update the user.

On the other hand, when you

await bus.Publish(new UserUpdatedEvent(...));

Rebus will generate a topic out of the event type - in your case, it'll be called something like "Messages.UserUpdatedEvent, UserUpdatedEvent", because Rebus defaults to use the short, assembly-qualified type name of the type being published - and publish the event to that topic.

To be able to receive events published to a topic, someone needs to subscribe to it. In your case, you'd simply let your subscriber

await bus.Subscribe<UserUpdatedEvent>();

and then it will start receiving the events. This is a publish-subscribe channel, which is handy for distributing events to 0..n subscribers.

I hope that made sense

  • Related