Azure ServiceBus 消息序列化/反序列化 [英] Azure ServiceBus Message Serialization/Deserialization

查看:42
本文介绍了Azure ServiceBus 消息序列化/反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .NET Core 应用程序通过 Azure 服务总线队列发送对象,并让 Web 作业(.NET Core 也是如此)接收它.

I am using a .NET Core application to send an object through an Azure Service Bus Queue and have it received by a Web Job (.NET Core as well.)

我的问题是如何序列化/反序列化来发送/接收对象?

My question is how to serialize/deserialize to send/receive the object?

我发现许多对旧 BroakerMessage.GetBody() 的引用以接收消息,但没有引用新的 .NET Core 方法.请指教,谢谢.

I found lots of references to the legacy BroakerMessage.GetBody() to receive the message, but not to the new .NET Core method. Please advise, thanks.

发件人代码:

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)
{
}

推荐答案

可以使用 JSON 序列化来启用这些对象/实体的传输.

It is possible to use JSON serialization to enable transferring these objects/entities.

假设以下类是将向 Azure 服务总线队列发送/从 Azure 服务总线队列接收的对象实例的类型:

Assume the following class is the type of which object instances will be sent to/received from an Azure Service Bus queue:

public class Customer{ public string Name { get; set; } public string Email { get; set; } }

--- 发送---

在下面找到发送客户对象实例的示例代码(.NET Core 2.0 控制台应用程序):

Find below a sample code (.NET Core 2.0 Console Application) to send a customer object instance:

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)示例代码以接收消息并对其进行反序列化:

Find below an Azure Function (Service Bus Queue Trigger/.NET Standard 2.0) sample code to receive the message and deserialize it:

[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 包:

The following NuGet packages were used/tested for the samples above:

  • Microsoft.Azure.ServiceBus(3.0.2 版).
  • Newtonsoft.Json(版本 11.0.2).

考虑阅读:在下面的 JSON.NET 性能提示文章中找到:https://www.newtonsoft.com/json/help/html/Performance.htm

Consider Reading: Find below the performance tips article for the JSON.NET: https://www.newtonsoft.com/json/help/html/Performance.htm

设计原理:在最新的 Microsoft.Azure.ServiceBus 中删除了内置 POCO 序列化支持.这是因为虽然这种隐藏的序列化魔法很方便,但应用程序应该显式控制对象序列化并将它们的对象图转换为流,然后再将它们包含到消息中,并在接收方执行相反的操作.这会产生可互操作的结果."

Design rationale: Built in POCO serialization support was removed in the latest Microsoft.Azure.ServiceBus. This was because "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

这篇关于Azure ServiceBus 消息序列化/反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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