我试图通过RabbitMQ获取rebus来检索我已成功发布到消息队列的消息。我不应该做什么,需要做什么。下面是示例代码?
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 WagonHandler : IHandleMessages
{
public async Task Handle(Wagon message)
{
Console.WriteLine($"Token {message.Token}");
Console.WriteLine($"WagonId {message.WagonId}");
}
}
答案 0 :(得分:2)
此代码
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所述),仅在应用程序关闭时进行处理。