Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing object messages in Azure Queue Storage

I'm trying to find a way to pass objects to the Azure Queue. I couldn't find a way to do this.

As I've seen I can pass string or byte array, which is not very comfortable for passing objects.

Is there anyway to pass custom objects to the Queue?

Thanks!

like image 960
Roman Avatar asked Dec 18 '11 08:12

Roman


People also ask

How do I send messages to Azure storage queue?

Quickstart: Create Azure Storage queues in the portal Use the Azure portal to create a queue. Then, use the Azure portal to add a message, view the message properties, and dequeue the message.

Is the message size limit in a Azure storage queue?

Azure documentation states that Storage queue message cannot exceed 64 KB. But in article that compares service bus and storage queues, it says that Azure supports large messages by combining queues and blobs – at which point you can enqueue up to 200 GB for a single item.

What is the maximum size of a single queue message in Azure queue storage?

A queue message can be up to 64 KB in size. A queue may contain millions of messages, up to the total capacity limit of a storage account.


2 Answers

You can use the following classes as example:

 [Serializable]     public abstract class BaseMessage     {         public byte[] ToBinary()         {             BinaryFormatter bf = new BinaryFormatter();             byte[] output = null;             using (MemoryStream ms = new MemoryStream())             {                 ms.Position = 0;                 bf.Serialize(ms, this);                 output = ms.GetBuffer();             }             return output;         }          public static T FromMessage<T>(CloudQueueMessage m)         {             byte[] buffer = m.AsBytes;             T returnValue = default(T);             using (MemoryStream ms = new MemoryStream(buffer))             {                 ms.Position = 0;                 BinaryFormatter bf = new BinaryFormatter();                 returnValue = (T)bf.Deserialize(ms);             }             return returnValue;         }     } 

Then a StdQueue (a Queue that is strongly typed):

   public class StdQueue<T> where T : BaseMessage, new()     {         protected CloudQueue queue;          public StdQueue(CloudQueue queue)         {             this.queue = queue;         }          public void AddMessage(T message)         {             CloudQueueMessage msg =             new CloudQueueMessage(message.ToBinary());             queue.AddMessage(msg);         }          public void DeleteMessage(CloudQueueMessage msg)         {             queue.DeleteMessage(msg);         }          public CloudQueueMessage GetMessage()         {             return queue.GetMessage(TimeSpan.FromSeconds(120));         }     } 

Then, all you have to do is to inherit the BaseMessage:

[Serializable] public class ParseTaskMessage : BaseMessage {     public Guid TaskId { get; set; }      public string BlobReferenceString { get; set; }      public DateTime TimeRequested { get; set; } } 

And make a queue that works with that message:

CloudStorageAccount acc;             if (!CloudStorageAccount.TryParse(connectionString, out acc))             {                 throw new ArgumentOutOfRangeException("connectionString", "Invalid connection string was introduced!");             }             CloudQueueClient clnt = acc.CreateCloudQueueClient();             CloudQueue queue = clnt.GetQueueReference(processQueue);             queue.CreateIfNotExist();             this._queue = new StdQueue<ParseTaskMessage>(queue); 

Hope this helps!

like image 146
astaykov Avatar answered Sep 17 '22 11:09

astaykov


Extension method that uses Newtonsoft.Json and async

    public static async Task AddMessageAsJsonAsync<T>(this CloudQueue cloudQueue, T objectToAdd)     {         var messageAsJson = JsonConvert.SerializeObject(objectToAdd);         var cloudQueueMessage = new CloudQueueMessage(messageAsJson);         await cloudQueue.AddMessageAsync(cloudQueueMessage);     } 
like image 37
Akodo_Shado Avatar answered Sep 21 '22 11:09

Akodo_Shado