Rebus RabbitMQ 无法订阅

Rebus RabbitMQ unable to subscribe

我正在尝试使用 RabbitMQ 重新调用以检索我已成功发布到消息队列的消息。我没有做的事情需要做。下面是示例代码?

static async Task MainSubscribeAsync()
        {

            var connection = "amqp://jhgj67546:yjyj5565@localhost";

            using (var activator = new BuiltinHandlerActivator())
            {
                activator.Register(() => new WagonHandler());

                var bus = Configure.With(activator)
                    .Logging(l => l.ColoredConsole())
                    .Transport(t => t.UseRabbitMq(connection, "wagon_v1")
                        .ExchangeNames(directExchangeName: "WamosExchange"))
                    .Start();

                await activator.Bus.Subscribe<Wagon>();

                Console.WriteLine("Done");
            }
        }

处理程序 class 看起来像这样

class WagonHandler : IHandleMessages
    {
        public async Task Handle(Wagon message)
        {
            Console.WriteLine($"Token {message.Token}");
            Console.WriteLine($"WagonId {message.WagonId}");
        }
    }

这段代码

static async Task MainSubscribeAsync()
{

    var connection = "amqp://jhgj67546:yjyj5565@localhost";

    using (var activator = new BuiltinHandlerActivator())
    {
        activator.Register(() => new WagonHandler());

        var bus = Configure.With(activator)
            .Logging(l => l.ColoredConsole())
            .Transport(t => t.UseRabbitMq(connection, "wagon_v1")
                .ExchangeNames(directExchangeName: "WamosExchange"))
            .Start();

        await activator.Bus.Subscribe<Wagon>();

        Console.WriteLine("Done");
    }
}

启动总线,建立订阅,然后立即关闭,因为 BuiltinHandlerActivator 已处理。

这意味着 你可能很幸运,可能会收到一些消息,但在大多数情况下你可能不会:)

您应该改为在应用程序启动时配置总线,然后在应用程序的整个生命周期内将其作为单例实例(如 the wiki page about Rebus' instance policies 所述),仅在应用程序关闭时处理它下来。