[英]Azure ServiceBus message from .net core does not parse in .net 4.6.1
I have a ServiceBus in Azure and a number of queues. 我在Azure中有一个ServiceBus,并且有许多队列。 I have one service written in .net core which sends data into the queue like so:
我有一个用.net核心编写的服务,该服务将数据发送到队列中,如下所示:
queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
string example = "Hello World!";
var message = new Message(Encoding.UTF8.GetBytes(example));
await queueClient.SendAsync(message);
Other service, written in .net framework 4.6.1 is listening on that same queue like so: 用.net Framework 4.6.1编写的其他服务正在侦听同一队列,如下所示:
queueClient.OnMessage(ProcessQMessage);
private void ProcessQMessage (BrokeredMessage receivedMessage) {
var tmpBytes = receivedMessage.GetBody<byte[]>();
...
}
For some reason i get an exception from receivedMessage.GetBody(). 由于某种原因,我从receiveMessage.GetBody()获得了异常。 I get the same exception trying to parse it to string and object as well:
我也尝试将其解析为字符串和对象时遇到相同的异常:
There was an error deserializing the object of type System.Byte[].
反序列化类型为System.Byte []的对象时出错。 The input source is not correctly formatted.
输入源的格式不正确。
I understand that the problem comes from the fact that core and framework work a little differently. 我知道问题出在核心和框架工作略有不同这一事实。 Didn't expect this to be a problem though.
没想到这是一个问题。
If the systems are running with old client, you will be likely stuck with XmlObjectSerializer
(data contract serializer) like our .netfull client. 如果系统使用旧客户端运行,则您可能会像我们的.netfull客户端一样被
XmlObjectSerializer
(数据协定序列化程序)所困扰。 But things will not be broken. 但是事情不会被打破。 Sample code is like below which is exactly what happens underneath in the .netfull client.
示例代码如下所示,这正是.netfull客户端中发生的情况。
var bytes = your bytes;
var serializer = DataContractBinarySerializer<byte[]>.Instance;
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, bytes);
var msg = new Message(stream.ToArray());
var client = new Microsoft.Azure.ServiceBus.QueueClient(ConnectionString, Queue);
await client.SendAsync(msg);
await client.CloseAsync();
}
For more details, you could refer to this article and this issue . 有关更多详细信息,您可以参考本文和本期 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.