从RabbitMQ连接并接收消息后,它们不会从队列中删除。
每次连接时,我都会收到相同的消息,只有一个。
using(bus = RabbitHutch.CreateBus("host=localhost"))
{
bus.Receive<MyMessage>("my-queue", message => Console.WriteLine("MyMessage: {0}", message.Text));
}
答案 0 :(得分:1)
如Connecting to RabbitMQ中所述,创建Bus
的单个实例并在整个应用程序中使用它。
我确定了以下过程:
这是简化的Startup.cs
,其中的相关代码段为演示代码:
public class Startup
{
// This will be your application instance
IBus bus;
public void ConfigureServices(IServiceCollection services)
{
// Create, assign the Bus, and add it as Singleton to your application
bus = RabbitHutch.CreateBus("host=localhost");
// now you can easyly inject in your components
services.AddSingleton(bus);
}
public void Configure(IHostApplicationLifetime lifetime)
{
// Start receiving messages from the queue
bus.Receive<MyMessage>("my-queue", message => Console.WriteLine("MyMessage: {0}", message.Text));
// Hook your custom shutdown the the lifecycle
lifetime.ApplicationStopping.Register(OnShutdown);
}
private void OnShutdown()
{
// Dispose the Bus
bus.Dispose();
}
}