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

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

问题描述

我正在使用.NET Core应用程序通过Azure Service Bus队列发送对象,并通过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服务总线队列或从中接收对象实例的类型:

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函数(Service Bus Queue触发器/.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

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

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/zh-CN/azure/service-bus-messaging/service-bus-messages-payloads

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

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