EasyNetQ 未确认收到的消息
EasyNetQ is not acknowledging received messages
连接并从 RabbitMQ 接收消息后,它们不会从队列中删除。
每次连接时,我都会收到相同的消息,而且只有一条。
using(bus = RabbitHutch.CreateBus("host=localhost"))
{
bus.Receive<MyMessage>("my-queue", message => Console.WriteLine("MyMessage: {0}", message.Text));
}
如 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();
}
}
连接并从 RabbitMQ 接收消息后,它们不会从队列中删除。
每次连接时,我都会收到相同的消息,而且只有一条。
using(bus = RabbitHutch.CreateBus("host=localhost"))
{
bus.Receive<MyMessage>("my-queue", message => Console.WriteLine("MyMessage: {0}", message.Text));
}
如 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();
}
}