fix:mq消费未ack

This commit is contained in:
xiaolipro 2025-07-29 14:58:46 +08:00
parent 974ad920dd
commit 34e99f0a7e

View File

@ -29,7 +29,11 @@ public class RabbitClient(
}
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.Received += async (model, ea) =>
consumer.Received += (model, ea) =>
{
_ = Task.Run(async () =>
{
try
{
await using var scope = serviceScopeFactory.CreateAsyncScope();
foreach (var handler in scope.ServiceProvider.GetServices<IRabbitHandler>())
@ -39,6 +43,16 @@ public class RabbitClient(
await handler.Handle(ea);
}
}
channel.BasicAck(ea.DeliveryTag, false);
}
catch (Exception ex)
{
logger.LogError($"rabbit on queue({consumeOptions.Queue}) received error: {ex}");
channel.BasicNack(ea.DeliveryTag, false, consumeOptions.FailedRequeue);
}
});
return Task.CompletedTask;
};
channel.CallbackException += (sender, args) =>