Deepak Yadav
Deepak Yadav

Reputation: 51

RabbitMQ-MassTransit ReceiveEndPoint not able to listen to the Queue in .net core web APIs

I am using RabbitMQ MassTransit for service bus implementation in my .Net core solution. I have created a queue by the name log.service. After lot many efforts I was finally able to push the messages in the queue and can see them in management tool but when I am listening to the same queue in another microservice project, I am unable to do so. I have pushed the messages in the bus from Authentication service and want to log the event in Logging service. Please help! Here is my authentication-StartUp.cs

var buildr = new ContainerBuilder();
        buildr.RegisterType<LoggingCommandConsumer>();
        buildr.Register(c =>
            {
                return Bus.Factory.CreateUsingRabbitMq(sbc =>
                 {
                     var host = sbc.Host(new Uri("rabbitmq://localhost/"), h =>
                      {
                          h.Username("guest");
                          h.Password("guest");
                      });

                     sbc.ExchangeType = ExchangeType.Direct;
                     sbc.ReceiveEndpoint(host, "log.service", e =>
                                   {

                                       e.Consumer<LoggingCommandConsumer>();

                                   });
                 });

            })
            .As<IBusControl>()
            .As<IBus>()
            .As<IPublishEndpoint>()
            .SingleInstance();

        buildr.Populate(services);
        ApplicationContainer = buildr.Build();

        return new AutofacServiceProvider(ApplicationContainer);

Here is my logging-StartUp.cs:

 var buildr = new ContainerBuilder();
        buildr.RegisterType<LoggingCommandConsumer>();
        buildr.Register(context =>
            {
                var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
                 {
                     var host = cfg.Host(new Uri("rabbitmq://localhost/"), h =>
                       {
                           h.Username("guest");
                           h.Password("guest");
                       });

                     cfg.ReceiveEndpoint(host, "log.service", e =>
                                             {


                                                 e.Consumer<LoggingCommandConsumer>();

                                             });

                 });
                return busControl;
            })
           .SingleInstance()
            .As<IBusControl>()
            .As<IBus>();

        buildr.Populate(services);
        ApplicationContainer = buildr.Build();

        return new AutofacServiceProvider(ApplicationContainer);

Here I am starting the Bus in both the StartUp.CS

 var bus = ApplicationContainer.Resolve<IBusControl>();
        var busHandle = TaskUtil.Await(() => bus.StartAsync());
        lifetime.ApplicationStopping.Register(() => busHandle.Stop());

Here I am sending the message to end points from authentication controller:

    var sendToUri = new 
  Uri("rabbitmq://localhost/log.servicebind=true&queue=log.service");
        var endPoint = await _bus.GetSendEndpoint(sendToUri);

        await endPoint.Send<ILoggingCommand>(new
        {
            XCorrelationId = "asd",
            M4SId = "M4SId",
            Host = "asdasd",
            Level = "Level",
            Time = "2019-01-02T07:06:43.722Z",
            Message = "Message",
            Other = "Other"
        });

        return Ok();

When I try to get the above message in log.service bus in rabbitMQ management tool, I am able to do so...but not able to listen it in logging-startup.cs

Upvotes: 0

Views: 3815

Answers (1)

Alexey Zimarev
Alexey Zimarev

Reputation: 19630

Your endpoint has a queue defined by "log.service" + Guid.NewGuid().ToString() but you send messages to the log.service queue.

I don't really see the point of adding the guid to the endpoint address, what are you trying to achieve?

if you define your endpoint as cfg.ReceiveEndpoint(host, "log.service", ep => <configuration> it should work. You need to uncomment your consumer.

Upvotes: 2

Related Questions