Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure ServiceBus Message Serialization/Deserialization

Tags:

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)
{
}
like image 716
Username Not Exist Avatar asked Jun 06 '18 00:06

Username Not Exist


People also ask

What is Service Bus in Azure?

Azure Service Bus is a fully managed enterprise message broker with message queues and publish-subscribe topics (in a namespace). Service Bus is used to decouple applications and services from each other, providing the following benefits: Load-balancing work across competing workers.

How do I read messages from Service Bus queue?

Open Queue. Select Service Bus Explorer (preview) under Settings. Write policy name and select the checkbox to listen and click Create. Select the policy name and copy Primary or Secondary Connection String.

What is the underlying protocol of Azure Service Bus for sending and receiving messages?

The official Azure SDKs generally use the AMQP protocol for sending and receiving messages from Service Bus.


2 Answers

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

like image 108
Evandro de Paula Avatar answered Sep 24 '22 11:09

Evandro de Paula


how to serialize/deserialize to send/receive the object?

Please refer to the demo code blow:

Send message:

var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
await  queueClient.SendAsync(new Message { Body = body, ContentType = "text/plain" });

In the .net core WebJob

var body = Encoding.UTF8.GetString(message.Body);
var obj = JsonConvert.DeserializeObject<T>(body);

Test Result:

enter image description here

like image 42
Tom Sun - MSFT Avatar answered Sep 21 '22 11:09

Tom Sun - MSFT