Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

c# - Azure ServiceBus Message Serialization/Deserialization

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?

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.

Sender code:

using Microsoft.Azure.ServiceBus;

MyClass object = new MyClass();
var message = new Message(object);
await queueClient.SendAsync(message);

Receiver code:

using Microsoft.Azure.ServiceBus;

public void ProcessQueueMessage([ServiceBusTrigger("queue")] Message message, TextWriter log)
{
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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; } }

--- Send ---

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);

--- Receive ---

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

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

  • Microsoft.Azure.ServiceBus (version 3.0.2).
  • Newtonsoft.Json (version 11.0.2).

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


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...