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 26, 2019 in Azure by sabby
• 4,390 points
3,506 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.

Hope it helped!

If you need to know more about Azure, then you should join Azure training course online today.

Thank you!!

answered Mar 26, 2019 by Prerna
• 1,960 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
4,564 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
1,571 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 22, 2018 in Azure by club_seesharp
• 3,450 points
3,719 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
3,566 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 Jul 23, 2019 in Career Counselling by Gitika
• 65,890 points
1,881 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 Jul 23, 2019 in Career Counselling by Gitika
• 65,890 points
16,281 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 Jul 23, 2019 in Career Counselling by Gitika
• 65,890 points
1,174 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, 2019 in Azure by Prerna
• 1,960 points
3,815 views
0 votes
1 answer

How to install Windows Azure Storage Emulator?

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

answered Mar 7, 2019 in Azure by Prerna
• 1,960 points
2,738 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP