Azure 服务总线 - 使用 BrokeredMessage.GetBody 读取 .NET Core 2 发送的消息
Azure service bus - Read messages sent by .NET Core 2 with BrokeredMessage.GetBody
我正在将 .NET Core 2 用于需要将消息放在服务总线上并由旧版 .NET 4.6 接收器读取的应用程序。接收方也监听来自其他遗留应用程序的消息。
旧发件人:
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 发件人: 如
中所述
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);
但是,接收方无法反序列化消息并抛出以下错误
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 二进制 Reader 来反序列化消息。所以你的发送部分应该是这样的:
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());
我们可以直接从.net core端发送serialize json Object string,我们可以在.net端用下面的代码得到消息。它在我这边工作正常。
var dataObj = message.GetBody<UserData>(new DataContractJsonSerializer(typeof(UserData)));
.net core端发送消息代码:
var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(objUserData));
_client.SendAsync(new Message{Body = body,ContentType = "text/plain"}).Wait();
.net端接收消息代码:
var dataObj = message.GetBody<UserData>(new DataContractJsonSerializer(typeof(UserData)));
我也遇到了同样的问题,但是都没有解决。对我来说,下面的代码有效
// model is the object that you want to sent to Topic
Model model = new Model();
var serializator = new DataContractSerializer(typeof(string));
var json = JsonConvert.SerializeObject(model);
var memoryStream = new MemoryStream();
XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(memoryStream);
serializator.WriteObject(binaryDictionaryWriter, json);
binaryDictionaryWriter.Flush();
// below message can be sent to Topic via .NET Core and will be properly deserialized in .NET Framework subscriber
var message = new Message(memoryStream.ToArray());
var client = new TopicClient(_endpoint, _channelName );
await client.SendAsync(message);
我正在将 .NET Core 2 用于需要将消息放在服务总线上并由旧版 .NET 4.6 接收器读取的应用程序。接收方也监听来自其他遗留应用程序的消息。
旧发件人:
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 发件人: 如
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);
但是,接收方无法反序列化消息并抛出以下错误
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 二进制 Reader 来反序列化消息。所以你的发送部分应该是这样的:
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());
我们可以直接从.net core端发送serialize json Object string,我们可以在.net端用下面的代码得到消息。它在我这边工作正常。
var dataObj = message.GetBody<UserData>(new DataContractJsonSerializer(typeof(UserData)));
.net core端发送消息代码:
var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(objUserData));
_client.SendAsync(new Message{Body = body,ContentType = "text/plain"}).Wait();
.net端接收消息代码:
var dataObj = message.GetBody<UserData>(new DataContractJsonSerializer(typeof(UserData)));
我也遇到了同样的问题,但是都没有解决。对我来说,下面的代码有效
// model is the object that you want to sent to Topic
Model model = new Model();
var serializator = new DataContractSerializer(typeof(string));
var json = JsonConvert.SerializeObject(model);
var memoryStream = new MemoryStream();
XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(memoryStream);
serializator.WriteObject(binaryDictionaryWriter, json);
binaryDictionaryWriter.Flush();
// below message can be sent to Topic via .NET Core and will be properly deserialized in .NET Framework subscriber
var message = new Message(memoryStream.ToArray());
var client = new TopicClient(_endpoint, _channelName );
await client.SendAsync(message);