RabbitMQ-MassTransit ReceiveEndPoint无法在.net核心Web API中监听队列

时间:2019-01-15 06:52:49

标签: c# rabbitmq masstransit

我正在使用RabbitMQ MassTransit在.Net核心解决方案中实现服务总线。我已经通过名称log.service创建了一个队列。经过大量的努力,我终于能够将消息推送到队列中并可以在管理工具中看到它们,但是当我在另一个微服务项目中侦听相同的队列时,我无法这样做。我已将邮件从身份验证服务推送到总线中,并希望将事件记录在日志记录服务中。请帮忙! 这是我的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);

这是我的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);

在这里,我要在两个StartUp.CS中启动总线

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

在这里,我正在从身份验证控制器将消息发送到端点:

    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();

当我尝试在rabbitMQ管理工具的log.service总线中获取上述消息时,我可以这样做...但是无法在logging-startup.cs中收听

1 个答案:

答案 0 :(得分:1)

您的端点具有由"log.service" + Guid.NewGuid().ToString()定义的队列,但是您将消息发送到log.service队列。

我真的看不到将guid添加到端点地址的意义,您要达到什么目的?

如果将端点定义为cfg.ReceiveEndpoint(host, "log.service", ep => <configuration>,则应该可以使用。您需要取消对消费者的评论。