调整RabbitMQ客户端版本并兼容同步调用

This commit is contained in:
2025-12-27 10:25:18 +08:00
parent c9980ef237
commit 7aeef0a24d
4 changed files with 22 additions and 23 deletions

View File

@@ -14,7 +14,7 @@ public sealed class RabbitMqMessagePublisher(RabbitMqConnectionFactory connectio
: IMessagePublisher, IAsyncDisposable
{
private IConnection? _connection;
private IChannel? _channel;
private IModel? _channel;
private bool _disposed;
/// <inheritdoc />
@@ -26,18 +26,16 @@ public sealed class RabbitMqMessagePublisher(RabbitMqConnectionFactory connectio
var channel = _channel ?? throw new InvalidOperationException("RabbitMQ channel is not available.");
// 2. 声明交换机
await channel.ExchangeDeclareAsync(options.Exchange, options.ExchangeType, durable: true, autoDelete: false, cancellationToken: cancellationToken);
channel.ExchangeDeclare(options.Exchange, options.ExchangeType, durable: true, autoDelete: false);
// 3. 序列化消息并设置属性
var body = serializer.Serialize(message);
var props = new BasicProperties
{
ContentType = "application/json",
DeliveryMode = DeliveryModes.Persistent,
MessageId = Guid.NewGuid().ToString("N")
};
var props = channel.CreateBasicProperties();
props.ContentType = "application/json";
props.DeliveryMode = 2;
props.MessageId = Guid.NewGuid().ToString("N");
// 4. 发布消息
await channel.BasicPublishAsync(options.Exchange, routingKey, mandatory: false, props, body, cancellationToken);
channel.BasicPublish(options.Exchange, routingKey, mandatory: false, basicProperties: props, body: body);
logger.LogDebug("发布消息到交换机 {Exchange} RoutingKey {RoutingKey}", options.Exchange, routingKey);
}
@@ -49,7 +47,7 @@ public sealed class RabbitMqMessagePublisher(RabbitMqConnectionFactory connectio
}
_connection ??= await connectionFactory.CreateConnectionAsync(cancellationToken);
_channel = await _connection.CreateChannelAsync(cancellationToken: cancellationToken);
_channel = _connection.CreateModel();
}
/// <summary>