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

查看:134
本文介绍了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.

我该怎么办

推荐答案

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天全站免登陆