.NET Core 5使用Apache.NMS.AMQP发布消息时类型转换错误求助
.NET Core 5 中使用 Apache.NMS.AMQP 发布 ActiveMQ Artemis 主题消息时类型转换错误解决方法
这个问题是由于Apache.NMS.AMQP库与ActiveMQ Artemis在AMQP协议的delivery mode字段类型处理不兼容导致的,以下是可行的解决方式:
解决步骤
直接移除对Producer的DeliveryMode设置,转而在创建消息时指定持久化属性,修改后的代码如下:
var _factory = new Apache.NMS.AMQP.ConnectionFactory(_connectURI); IConnection connection = _factory.CreateConnection(username,password); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); // 创建消息时直接指定持久化属性 ITextMessage msg = session.CreateTextMessage(stringMessage); msg.NMSDeliveryMode = MsgDeliveryMode.Persistent; var topicDestination = SessionUtil.GetTopic(session, topic); connection.Start(); IMessageProducer publisher = session.CreateProducer(topicDestination); // 移除publisher.DeliveryMode = MsgDeliveryMode.Persistent;这一行 publisher.Send(msg);
问题原因
当你手动设置publisher.DeliveryMode时,Apache.NMS.AMQP库会将该值以无符号字节(UnsignedByte)的格式发送给Broker,但ActiveMQ Artemis的AMQP处理模块期望接收的是有符号字节(byte),类型不匹配直接触发了类型转换异常。而直接设置消息本身的NMSDeliveryMode属性时,库内部会以符合Artemis预期的格式传递参数,规避了这个兼容性问题。
额外排查建议
- 确认使用的Apache.NMS.AMQP库是最新稳定版本,旧版本可能存在更多协议兼容性问题
- 检查ActiveMQ Artemis的AMQP acceptor配置,确保没有启用强制特定消息属性格式的约束
内容的提问来源于stack exchange,提问作者Bilal Abdulkany
相关产品推荐
相关产品推荐

