Azure 服务总线消息 Serialization/Deserialization
Azure ServiceBus Message Serialization/Deserialization
我正在使用 .NET Core 应用程序通过 Azure 服务总线队列发送对象并让 Web 作业接收它(还有 .NET Core。)
我的问题是如何serialize/deserialize到send/receive对象?
我发现很多对遗留 BroakerMessage.GetBody()
的引用以接收消息,但没有引用新的 .NET Core 方法。请指教,谢谢
发件人代码:
using Microsoft.Azure.ServiceBus;
MyClass object = new MyClass();
var message = new Message(object);
await queueClient.SendAsync(message);
收件人代码:
using Microsoft.Azure.ServiceBus;
public void ProcessQueueMessage([ServiceBusTrigger("queue")] Message message, TextWriter log)
{
}
how to serialize/deserialize to send/receive the object?
请参考下面的demo代码:
发送消息:
var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
await queueClient.SendAsync(new Message { Body = body, ContentType = "text/plain" });
在 .net 核心 WebJob
var body = Encoding.UTF8.GetString(message.Body);
var obj = JsonConvert.DeserializeObject<T>(body);
测试结果:
可以使用 JSON 序列化来传输这些 objects/entities。
假设以下 class 是将从 Azure 服务总线队列 to/received 发送的对象实例的类型:
public class Customer{ public string Name { get; set; } public string Email { get; set; } }
---发送---
在下方找到发送客户对象实例的示例代码(.NET Core 2.0 控制台应用程序):
QueueClient queueClient = new QueueClient(connectionString, queueName);
string messageBody = JsonConvert.SerializeObject(obj);
Message message = new Message(Encoding.UTF8.GetBytes(messageBody))
{
SessionId = sessionId
};
await queueClient.SendAsync(message);
---接收---
在下面找到一个 Azure 函数(服务总线队列触发器/.NET Standard 2.0)示例代码来接收消息并将其反序列化:
[FunctionName("ServiceBusQueueFunction")]
public static void Run([ServiceBusTrigger("taskqueue", Connection = "ServiceBusConnectionString")] Message message, TraceWriter log)
{
Customer customer = JsonConvert.DeserializeObject<Customer>(Encoding.UTF8.GetString(message.Body));
}
以下 NuGet 包 used/tested 用于上述示例:
- Microsoft.Azure.ServiceBus(版本 3.0.2)。
- Newtonsoft.Json(版本 11.0.2)。
考虑阅读: 在下面找到 JSON.NET 的性能提示文章:
https://www.newtonsoft.com/json/help/html/Performance.htm
设计原理:内置的 POCO 序列化支持已在最新 Microsoft.Azure.ServiceBus 中删除。这是因为 "while this hidden serialization magic is convenient, applications should take explicit control of object serialization and turn their object graphs into streams before including them into a message, and do the reverse on the receiver side. This yields interoperable results."
https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messages-payloads
这些都不适合我,因为:
当我们尝试将消息正文解析为 JSON 时出现异常,因为我们收到的消息正文是
"@\u0006string\b3http://schemas.microsoft.com/2003/10/Serialization/?\u000b{ \"a\": \"1\"}"
这是因为"Brokered Message Initializes a new instance of the BrokeredMessage class from a given object by using DataContractSerializer with a binary XmlDictionaryWriter."
参考:https://www.bfcamara.com/post/84113031238/send-a-message-to-an-azure-service-bus-queue-with
所以我改用这个博客 post:https://abhishekrlal.com/2012/03/30/formatting-the-content-for-service-bus-messages/
示例 1:使用字符串
使用字符串和默认 (DataContract + Binary) 序列化程序创建 BrokeredMessage 时:
BrokeredMessage stringDefaultMessage = new BrokeredMessage("default string");
您可以通过以下方式收到此消息:
string s = receiveMessage.GetBody<string>();
我正在使用 .NET Core 应用程序通过 Azure 服务总线队列发送对象并让 Web 作业接收它(还有 .NET Core。)
我的问题是如何serialize/deserialize到send/receive对象?
我发现很多对遗留 BroakerMessage.GetBody()
的引用以接收消息,但没有引用新的 .NET Core 方法。请指教,谢谢
发件人代码:
using Microsoft.Azure.ServiceBus;
MyClass object = new MyClass();
var message = new Message(object);
await queueClient.SendAsync(message);
收件人代码:
using Microsoft.Azure.ServiceBus;
public void ProcessQueueMessage([ServiceBusTrigger("queue")] Message message, TextWriter log)
{
}
how to serialize/deserialize to send/receive the object?
请参考下面的demo代码:
发送消息:
var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
await queueClient.SendAsync(new Message { Body = body, ContentType = "text/plain" });
在 .net 核心 WebJob
var body = Encoding.UTF8.GetString(message.Body);
var obj = JsonConvert.DeserializeObject<T>(body);
测试结果:
可以使用 JSON 序列化来传输这些 objects/entities。
假设以下 class 是将从 Azure 服务总线队列 to/received 发送的对象实例的类型:
public class Customer{ public string Name { get; set; } public string Email { get; set; } }
---发送---
在下方找到发送客户对象实例的示例代码(.NET Core 2.0 控制台应用程序):
QueueClient queueClient = new QueueClient(connectionString, queueName);
string messageBody = JsonConvert.SerializeObject(obj);
Message message = new Message(Encoding.UTF8.GetBytes(messageBody))
{
SessionId = sessionId
};
await queueClient.SendAsync(message);
---接收---
在下面找到一个 Azure 函数(服务总线队列触发器/.NET Standard 2.0)示例代码来接收消息并将其反序列化:
[FunctionName("ServiceBusQueueFunction")]
public static void Run([ServiceBusTrigger("taskqueue", Connection = "ServiceBusConnectionString")] Message message, TraceWriter log)
{
Customer customer = JsonConvert.DeserializeObject<Customer>(Encoding.UTF8.GetString(message.Body));
}
以下 NuGet 包 used/tested 用于上述示例:
- Microsoft.Azure.ServiceBus(版本 3.0.2)。
- Newtonsoft.Json(版本 11.0.2)。
考虑阅读: 在下面找到 JSON.NET 的性能提示文章: https://www.newtonsoft.com/json/help/html/Performance.htm
设计原理:内置的 POCO 序列化支持已在最新 Microsoft.Azure.ServiceBus 中删除。这是因为 "while this hidden serialization magic is convenient, applications should take explicit control of object serialization and turn their object graphs into streams before including them into a message, and do the reverse on the receiver side. This yields interoperable results."
https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messages-payloads
这些都不适合我,因为:
当我们尝试将消息正文解析为 JSON 时出现异常,因为我们收到的消息正文是
"@\u0006string\b3http://schemas.microsoft.com/2003/10/Serialization/?\u000b{ \"a\": \"1\"}"
这是因为"Brokered Message Initializes a new instance of the BrokeredMessage class from a given object by using DataContractSerializer with a binary XmlDictionaryWriter."
参考:https://www.bfcamara.com/post/84113031238/send-a-message-to-an-azure-service-bus-queue-with
所以我改用这个博客 post:https://abhishekrlal.com/2012/03/30/formatting-the-content-for-service-bus-messages/
示例 1:使用字符串
使用字符串和默认 (DataContract + Binary) 序列化程序创建 BrokeredMessage 时:
BrokeredMessage stringDefaultMessage = new BrokeredMessage("default string");
您可以通过以下方式收到此消息:
string s = receiveMessage.GetBody<string>();