chore: 消除构建警告并升级依赖

This commit is contained in:
2025-12-02 11:18:38 +08:00
parent e8777faf71
commit 3e01943727
4 changed files with 20 additions and 13 deletions

View File

@@ -25,14 +25,15 @@ public sealed class RabbitMqMessagePublisher(RabbitMqConnectionFactory connectio
EnsureChannel();
var options = optionsMonitor.CurrentValue;
_channel!.ExchangeDeclare(options.Exchange, options.ExchangeType, durable: true, autoDelete: false);
var channel = _channel ?? throw new InvalidOperationException("RabbitMQ channel is not available.");
channel.ExchangeDeclare(options.Exchange, options.ExchangeType, durable: true, autoDelete: false);
var body = serializer.Serialize(message);
var props = _channel.CreateBasicProperties();
var props = channel.CreateBasicProperties();
props.ContentType = "application/json";
props.DeliveryMode = 2;
props.MessageId = Guid.NewGuid().ToString("N");
_channel.BasicPublish(options.Exchange, routingKey, props, body);
channel.BasicPublish(options.Exchange, routingKey, props, body);
logger.LogDebug("发布消息到交换机 {Exchange} RoutingKey {RoutingKey}", options.Exchange, routingKey);
return Task.CompletedTask;
}

View File

@@ -24,18 +24,20 @@ public sealed class RabbitMqMessageSubscriber(RabbitMqConnectionFactory connecti
EnsureChannel();
var options = optionsMonitor.CurrentValue;
_channel!.ExchangeDeclare(options.Exchange, options.ExchangeType, durable: true, autoDelete: false);
_channel.QueueDeclare(queue, durable: true, exclusive: false, autoDelete: false);
_channel.QueueBind(queue, options.Exchange, routingKey);
_channel.BasicQos(0, options.PrefetchCount, global: false);
var channel = _channel ?? throw new InvalidOperationException("RabbitMQ channel is not available.");
var consumer = new AsyncEventingBasicConsumer(_channel);
channel.ExchangeDeclare(options.Exchange, options.ExchangeType, durable: true, autoDelete: false);
channel.QueueDeclare(queue, durable: true, exclusive: false, autoDelete: false);
channel.QueueBind(queue, options.Exchange, routingKey);
channel.BasicQos(0, options.PrefetchCount, global: false);
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.Received += async (_, ea) =>
{
var message = serializer.Deserialize<T>(ea.Body.ToArray());
if (message == null)
{
_channel.BasicAck(ea.DeliveryTag, multiple: false);
channel.BasicAck(ea.DeliveryTag, multiple: false);
return;
}
@@ -51,15 +53,15 @@ public sealed class RabbitMqMessageSubscriber(RabbitMqConnectionFactory connecti
if (success)
{
_channel.BasicAck(ea.DeliveryTag, multiple: false);
channel.BasicAck(ea.DeliveryTag, multiple: false);
}
else
{
_channel.BasicNack(ea.DeliveryTag, multiple: false, requeue: false);
channel.BasicNack(ea.DeliveryTag, multiple: false, requeue: false);
}
};
_channel.BasicConsume(queue, autoAck: false, consumer);
channel.BasicConsume(queue, autoAck: false, consumer);
await Task.CompletedTask.ConfigureAwait(false);
}