Brendan Enrick

Daily Software Development

Default Azure Storage Information

The cloud is nice and useful, but there is one feature which is vitally important to most applications in my opinion. The storage which is used in Azure pretty much will need to be used by every application. One of the reasons for this is because there is a little bit of management of processes which needs to be done. This I've noticed requires a combination of the Queues and the Tables.

Queues are great because roles are able to queue up work which needs to be done. This helps make sure that all required tasks can be completed. Without these communication between the processes could get a bit messy. Also the information being worked with needs to be stored in a shared location which all of the roles and their instances have access to. The storage makes this work.

For these apps you don't use the standard configuration files to store your config settings. There is a file in the Cloud project called ServiceConfiguration.csfg, and this file is where the ConfigurationSection is located. This is where you define the settings for connecting to your storage when it is in the cloud. You will also have your configuration information stored in your web.config, and this will make things nice and easy. Since there are methods to get your configuration information which know to check the correct location.

For now these connections are made using http, so these "endpoints" are simply URLs. For local work the dev fabric and the storage are local. Because of this everyone has the same default settings for their storage. A lot of people are probably going to be looking for these default settings so to make it easy to find, here they are.

These are the AppSettings ones in the normal configuration file.

  <add key="AccountName" value="devstoreaccount1"/>
  <add key="AccountKey" value="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="/>
  <add key="BlobStorageEndpoint" value="http://localhost:10000"/>
  <add key="QueueStorageEndpoint" value="http://localhost:10001"/>
  <add key="TableStorageEndpoint" value="http://localhost:10002"/>

These are the ones in the ServiceConfiguration.csfg file.

  <Setting name="BlobStorageEndpoint" value="" />
  <Setting name="QueueStorageEndpoint" value="" />
  <Setting name="TableStorageEndpoint" value="" />
  <Setting name="AccountName" value="devstoreaccount1"/>
  <Setting name="AccountSharedKey" value="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="/>

If you need access in both a WebRole and a WorkerRole this config section can be added to each one.

Also because you have added this to that configuration file you will need to add the definition of these to the ServiceDefinition.csdef file in the following way.

  <Setting name="BlobStorageEndpoint"/>
  <Setting name="QueueStorageEndpoint"/>
  <Setting name="TableStorageEndpoint"/>
  <Setting name="AccountName"/>
  <Setting name="AccountSharedKey"/>

If you don't have this file matching the configuration you will get this nasty error message which doesn't tell you much. Thanks for telling me "Invalid configuration file." It doesn't say what file or what is wrong or anything.

Invalid configuration file

When you need to access Azure storage you will be using some of this information. Some helper methods have been created which access these values from your configuration. These will help you get StorageAccountInfo classes. You can use them in this way.

StorageAccountInfo queueAccount = StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration();
StorageAccountInfo blobAccount = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration();
StorageAccountInfo tableAccount = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();

UPDATE: I forgot to mention when I first posted this that the Storage classes for this early release of Windows Azure are kept in one of the sample projects. It is the StorageClient sample project and the namespace needed is Microsoft.Samples.ServiceHosting.StorageClient. This will change once they've gotten some more substantial libraries set up for accessing Azure Storage.

This keeps things nice and clean, and makes connecting a lot easier.

Once you have these AccountInfo objects you can use them in the constructors for your service classes which give you access to the storage. You can access that storage using the following code.

QueueStorage queueService = QueueStorage.Create(queueAccount);
BlobStorage blobService = BlobStorage.Create(blobAccount);
TableStorage tableService = TableStorage.Create(tableAccount);
TableStorage tableService = TableStorage.CreateTablesFromModel(typeof(MyClass), tableAccount);

Please note that the table storage is slightly different from the other two. They are much easier since they don't vary in structure. I recommend you create that service based on a specific model class you have already defined. It should inherit from the TableStorageEntity class.

Now you're ready to start working with Azure storage.