How to pass object messages in Azure Queue Storage?

0 votes
I'm trying to find a way to pass objects to the Azure Queue. Is there anyway to pass custom objects to the Queue?
Mar 25 in Azure by sabby
• 4,350 points
118 views

1 answer to this question.

0 votes

I already have a base class, so I used this:

using System;
using System.Text;
using Microsoft.WindowsAzure.Storage.Queue;
using Newtonsoft.Json;

namespace Example.Queue
{
    public static class CloudQueueMessageExtensions
    {
        public static CloudQueueMessage Serialize(Object o)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(o.GetType().FullName);
            stringBuilder.Append(':');
            stringBuilder.Append(JsonConvert.SerializeObject(o));
            return new CloudQueueMessage(stringBuilder.ToString());
        }

        public static T Deserialize<T>(this CloudQueueMessage m)
        {
            int indexOf = m.AsString.IndexOf(':');

            if (indexOf <= 0)
                throw new Exception(string.Format("Cannot deserialize into object of type {0}", 
                    typeof (T).FullName));

            string typeName = m.AsString.Substring(0, indexOf);
            string json = m.AsString.Substring(indexOf + 1);

            if (typeName != typeof (T).FullName)
            {
                throw new Exception(string.Format("Cannot deserialize object of type {0} into one of type {1}", 
                    typeName,
                    typeof (T).FullName));
            }

            return JsonConvert.DeserializeObject<T>(json);
        }
    }
}

This approach is compact. If you use this code, you may still need a reference to the original CloudQueueMessage in order to Delete it from the queue after reading.

answered Mar 25 by Prerna
• 1,940 points

Related Questions In Azure

0 votes
1 answer

How to determine the number of messages in an Azure Services Bus Queue?

You will get error when you try ...READ MORE

answered Jun 12, 2018 in Azure by null_void
• 3,220 points
113 views
0 votes
1 answer

How to upload a file on to Azure Blob storage without writing a code?

You can find the below tools useful ...READ MORE

answered Apr 13, 2018 in Azure by club_seesharp
• 3,450 points
89 views
0 votes
1 answer

How can I remove/hide/disable excessive HTTP response headers in Azure/IIS7 without having to use UrlScan?

MSDN published an article on how to ...READ MORE

answered May 21, 2018 in Azure by club_seesharp
• 3,450 points
371 views
0 votes
1 answer

How can i upload to Azure Blob storage with Shared Access key?

For GetBlobReferenceFromServer to work, the blob must be present ...READ MORE

answered Jun 12, 2018 in Azure by club_seesharp
• 3,450 points
611 views
0 votes
3 answers

What is the job role of an Azure Developer?

Hi, Here are some job roles of an ...READ MORE

answered 17 hours ago in Career Counselling by Gitika
• 20,760 points
54 views
0 votes
4 answers

What are the skill required for an Azure Developer?

Hi, The skills required for an Azure Developer ...READ MORE

answered 17 hours ago in Career Counselling by Gitika
• 20,760 points
563 views
0 votes
3 answers

What is the salary offered to an Azure Developer?

Hi, For entry-level position starts at $87.750 per ...READ MORE

answered 17 hours ago in Career Counselling by Gitika
• 20,760 points
45 views
0 votes
2 answers
0 votes
1 answer

How to check if multiple files exist in Azure container?

foreach loop is the most efficient way though. ...READ MORE

answered Mar 4 in Azure by Prerna
• 1,940 points
55 views
0 votes
1 answer

How to install Windows Azure Storage Emulator?

There may be an issue with the ...READ MORE

answered Mar 6 in Azure by Prerna
• 1,940 points
36 views