in Windows Azure

Programming Windows Azure Table Storage

In my previous post, we went through the basics of Windows Azure Table Storage.

The sample application we are going to create is a small guestbook app which will allow your site visitors to enter some information.

Creating the Project

Open Visual Studio 2008 and create a Web Cloud Service project.
 

Updating Service Definition

The first step to do is to update the service definition and service configuration files to include the storage account. Edit your service definition file found in the Guestbook project and enter these settings

AccountName

This specifies your Windows Azure account name

AccountSharedKey

The shared key used to authenticate requests made against your Windows Azure account

TableStorageEndpoint

The base URI of the table storage service

Updating Service Configuration

After setting the properties in our service definition, we need to set the values of those properties in our service configuration file found in the Guestbook project


 
AccountName

devstoreaccount1

AccountSharedKey
 
Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==

TableStorageEndpoint

http://127.0.0.1:10002/

All the values reflect your local development storage and nothing in the cloud. The development storage supports only the above fixed account and is same for everyone.

We should be able to test our azure application locally in the local development storage before deploying it to the cloud.  Testing your application locally will also help you to debug your applications.

Creating  the Table Schema

Defining the table schema is as easy as creating classes. The idea here is to create a class, where the class name corresponds to the table name and the properties correspond to the table properties. The local development storage environment uses ADO.NET to create tables in the local environment, whereas this is not needed when we host our application in the cloud.

Creating these classes are made simple by using the StorageClient library provided in the Windows Azure SDK


 
You can add the StorageClient project to our Guestbook solution or just reference the library.

Below is our class which represents the Guestbook table:


 
As you can see in the above screenshot, our class GuestDataModel inherits TableStorageEntity. This TableStorageEntity class is available in the StorageClient library and defines some of the properties such as partition key and row key that is required for each entity in the table.

The GuestDataModel consists of three properties:

1) Message
2) Name
3) Url

And our simple constructor where we initialize the partition key and row key:


 
As the local storage uses ADO.NET we are going  to create class GuestDataServiceContext which inherits from  TableStorageDataServiceContext from the StorageClient library. The TableStorageDataServiceContext represents the runtime context of our ADO.NET service.


 
Now having the data model and the data context in place, its very easy to query our storage and perform the usual insert, delete operations

We create a GuestDataSource which is going to help interact with the data context and insert, delete and get all the GuestDataModel entities
 

As you can see, it’s pretty straight forward. Below are insert and delete methods:
 

Creating our Tables

We saw how to insert, delete and get all our entities from the table using a simple data source, but where are we actually creating the table?

We are going to create the table on the first request to the website which can be done by adding the appropriate code into the global application class.
 
Executing this guestbook sample

Executing this guestbook sample is just as same as executing a normal asp.net application! Hit F5 and Visual Studio 2008 will take care of everything – from starting the local development storage, creating the tables and debugging the application.

Below is a screenshot of our guestbook app:


 
Deploying Guestbook to the Cloud

To deploy this sample in the cloud, you should have a Windows Azure storage account. For more information on deploying a service on Windows Azure, visit here.

I have already deployed this application to the cloud. So, if you are interested to try it out, please visit http://guestbook.cloudapp.net

You can download the Guestbook sample below:
 

*Originally posted at Geekzone VS 2008 blog

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.

  1. Thanks for the details. Would like to know, how to create Azure Tables from a class that has a property of user defined type. for eg:I would like to create Azure Tables for Model1 class and insert values to it.
    public class Model1
    {
    public Model2 Prop1{get; set;};
    }

    where Model2 is
    public class Model2
    {
    public string prop1{get; set;};
    }

  2. Thanks for the details. Would like to know, how to create Azure Tables from a class that has a property of user defined type. for eg:I would like to create Azure Tables for Model1 class and insert values to it.
    public class Model1
    {
    public Model2 Prop1{get; set;};
    }

    where Model2 is
    public class Model2
    {
    public string prop1{get; set;};
    }