Azure Storage - 10: Azure Queue + .NET - Attributes and Metadata


In previous post I have described stored access policies and provided basic code samples. In this post I am going to discuss another topic - attributes and metadata. Attributes is simple properties, metadata for a queue is stored as name-value pairs.


Refer to previous posts to get started with Azure Storage Queue Service.


There are several properties that worth to be highlighted, the first one is ApproximateMessageCount, name says for itself. The property is null by default, you need to call FetchAttributes to get the value. The code should look something like this:

for (var i = 0; i < 10; i++)
    queue1.AddMessage(new CloudQueueMessage($"test{i}"));


This code will output the number of messages in the queue. You can read more about this property on MSDN

The next property is EncodeMessage, it controls whether the queue message will be base64 encoded/decoded before add/get. Consider the following example:

 queue.AddMessage(new CloudQueueMessage("message"));
queue.EncodeMessage = false;
var message = queue.GetMessage();

This code prints bWVzc2FnZQ==. It is because we told Azure Storage library to don't decode message on GetMessage. EncodeMessage enables developers to use any kind of content inside messages. Consider another example:

queue.EncodeMessage = false;
queue.AddMessage(new CloudQueueMessage(new byte[] { 0 }));

This code fails with the following exception:

EncodeMessage should be true for binary message.

And it is true, you can't pass unencoded binary data over internet using http(s) protocol. The following example shows another 'bad' code:

queue.EncodeMessage = false;
queue.AddMessage(new CloudQueueMessage("\0"));

This code fails with the following exception:

'.', hexadecimal value 0x00, is an invalid character.

Azure Storage library can even create the proper message if you use invalid characters.

You can probably set EncodeMessage to false in some high-load scenarios, but make sure you understand the consequences.

There are two more properties that have to be mentioned:

  • Uri - gets the URI of the queue.
  • StorageUri - gets the queue URIs for both the primary and secondary locations.

The metadata is stored in the Metadata dictionary. Consider the following example:

queue.Metadata.Add("key1", "value1");

It loads metadata from server and then adds a single key to it. It is very simple to manage the queue metadata. There are many use-cases where container-level storage can be used.


In this post I have described some of the Azure Storage queue object properties and provided a few code samples. In the next post I will switch to Azure Storage Table service.