Beantwortet Using type derived from CloudQueueMessage

  • יום רביעי 25 יולי 2012 04:58
     
      קוד כלול
    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?

כל התגובות

  • יום רביעי 25 יולי 2012 09:10
     
     תשובה

    go with that other approach you found, that's the way it meant to be. The queue message type is just a container to hold your data, which can be either plain text (string) or else a set of bytes (which will be encoded to make sure it can be transported in XML). Just take your own data structures (make sure they are serializable), use a serializer to get the raw data and pass as content into a CloudQueueMessage, on the other end you do the opposite way.

    Be careful if your data structures store a lot of data - CloudQueueMessage can only handle 64kB of payload, and since raw data is base64-encoded means about 1/3 of overhead, so max plain payload is about 48kB. There are couple approaches to deal with it, basically in such a case you'll rather store the serialized message somewhere else (blob storage) and just put a pointer to it into the queue message. Not a big deal, there are couple samples around as well.