Azure 服务总线 - 使用 BrokeredMessage.GetBody 读取 .NET Core 2 发送的消息 [英] Azure service bus - Read messages sent by .NET Core 2 with BrokeredMessage.GetBody

查看:27
本文介绍了Azure 服务总线 - 使用 BrokeredMessage.GetBody 读取 .NET Core 2 发送的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 .NET Core 2 用于需要在服务总线上放置消息并由旧版 .NET 4.6 接收器读取的应用程序.接收方也会监听来自其他遗留应用程序的消息.

I am using .NET Core 2 for an application which needs to put a message on the Service bus and read by a legacy .NET 4.6 receiver. The receiver listens to messages from other legacy applications as well.

旧发件人:

UserData obj = new UserData()
{
  id = 1,
  name = "Alisha"
};
BrokeredMessage message = new BrokeredMessage(consentInstated);
_client.Send(message);

旧接收器:

var dataObj = objBrokeredMessage.GetBody<UserData>();
businessFunc(dataObj.id, dataObj.name);

.NET Core 2 发送方:https://stackoverflow.com/a/45069423 中所述/1773900

var ser = new System.Runtime.Serialization.DataContractSerializer(typeof(UserData));
var ms = new MemoryStream();
ser.WriteObject(ms, objUserData);
var message = new Message(ms.ToArray());
_client.Send(message);

然而,接收者无法反序列化消息并抛出以下错误

However, the reciever fails to deserialize the message and throws the following error

System.Runtime.Serialization.SerializationException:有一个反序列化 UserData 类型的对象时出错.输入源是格式不正确.---> System.Xml.XmlException: 输入源格式不正确.

System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type UserData. The input source is not correctly formatted. ---> System.Xml.XmlException: The input source is not correctly formatted.

我该怎么做才能让两个发件人与同一个收件人一起工作?

What can I do to make both senders work with the same receiver?

推荐答案

BrokeredMessage 正在使用 XML Binary Reader 来反序列化消息.所以你的发送部分应该是这样的:

BrokeredMessage is using XML Binary Reader to deserialize the messages. So your sending part should look like this:

var ser = new DataContractSerializer(typeof(UserData));
var ms = new MemoryStream();
XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(ms);
ser.WriteObject(binaryDictionaryWriter, obj);
binaryDictionaryWriter.Flush();
var message = new Message(ms.ToArray());

这篇关于Azure 服务总线 - 使用 BrokeredMessage.GetBody 读取 .NET Core 2 发送的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆