1

I have this in my program,

string message = string.Empty;                
queue.AddMessage(new CloudQueueMessage(message));

I can queue strings, its working but what if I have my own model, how can I push it to my queue? Is there a way to convert this model to byte? Since I see that I can also push bytes.

By models I mean like this:

public class Region
{
    public string countryCode { get; set; }
    public string countryName { get; set; }
    public string region { get; set; }
}

How can I have a model to be pushed to the queue?

StuartLC
  • 104,537
  • 17
  • 209
  • 285
sclang
  • 229
  • 2
  • 15

2 Answers2

1

The unit of transfer on Azure Queue storage is the CloudQueueMessage

Cloud messages carry the payload of the message (i.e. your object or entity graph) in a serialized string (e.g. xml or json) or serialized binary representation (byte[]). You have serialization options such as:

The choice of payload serialization format will depend on what level of 'compactness' you need on the data, and the compatibility required by the technologies that downstream clients will be using.

Unless bandwidth and deserialization time is absolutely critical, I would recommend using Json as a general starting point, given its widespread adoption, and it is easy to read serialized message payloads.

Messages are then published with a method such as AddMessageAsync and consumed by GetMessageAsync

For publishing, you will need to serialize your classes / entity graphs there are CloudQueueMessage constructor overloads which accept byte[] or string parameters representing the message payload.

CloudQueueMessage(byte[])
CloudQueueMessage(string)

Similarly, the consumer will need to deserialize the payload received, which can be retrieved via either:

Also see: XmlSerialization example : Passing object messages in Azure Queue Storage

StuartLC
  • 104,537
  • 17
  • 209
  • 285
0

If you read and write your models the same in each app then you can share Serialization/Deserialization functions to convert your objects to string or byte[].

Ross Bush
  • 14,648
  • 2
  • 32
  • 55