Question

Possible Duplicate:
Passing object messages in Azure Queue Storage

I am working on a use case that requires my queue messages a few more properties than the ones provided by Windows Azure Queue Messages (CloudQueueMessage class). I can't use most of the properties in CloudQueueMessage as they are mostly protected.

So I thought of inheriting the CloudQueueMessage and add my extra properties to the derived class fails. My derived class looks like below:

public class AzureQueueMessage : CloudQueueMessage
{
      public AzureQueueMessage(string content): base(content)
      {
      }

      //My new property
      public string Label { get; set; }
}

Rest of my message insertion and retrieval code looks like below:

AzureQueueMessage message = new AzureQueueMessage("testing");
cloudQueue.AddMessage(message);
CloudQueueMessage qmessage = cloudQueue.GetMessage();
AzureQueueMessage azureMessage = qmessage as AzureQueueMessage;

Here, cloudQueue is my Azure Queue instance.

The message inserts fine, but azureMessage is always null as the cast back to my derived class.

I did come across a solution on similar lines here but I am yet to try it out.

Is this behavior normal or am I missing something?

Was it helpful?

Solution

Those are the basic principles of OOP. When you use the "as AzureQueueMessage" cast, you actually say: "Try to see if this is instance is actually an AzureQueueMessage". But GetMessage() does not return an AzureQueueMessage, it returns an CloudQueueMessage, and there's no way you can change that.

If you want to work with an instance of your AzureQueueMessage you'd have to create it afterwards yourself. You could write an constructor that accepts the returned CloudQueueMessage and takes over all values.

OTHER TIPS

What you want is totally achievable, but in a slightly different way. It is known as "strongly typed queues". Check out this SO question as it will fully answer your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top