Windows Azure Storage Table Simplified

With Windows Azure 1.3 release I am super excited to see the most desired features addition and new portal. I will be posting on new feature soon. What remains the same as backbone of Windows Azure is the storage mechanism - a robust, secured and accessible location to capture user data apart from regular SQL Azure. Here I have tried to make it simplified for all of us who is starting new.

What we want to achieve?

We want to create a table called REEntry having few columns Alias, TopicTitle, Agenda etc., and from ASP.NET application we will store and view them.

Create Windows Azure Application

image

image

Then after that in the Solution Explorer choose the project WebRole1 and right click to choose setting.

image

Add a new setting called “DataConnection” of type Connection String as Windows Azure Storage Emulator. This will use the local SQL Express table to store during development.

image

Data Layer Class

After that add a Class Library project to your solution called ReadinessEventDBLibrary. Here we will be creating all our Entity, Context and Public methods.

We need to refer to assemblies here Microsoft.WindowsAzure.StorageClient and System.Data.Services.Client.

After that we need to use the below using block,

Using Block

  1. using Microsoft.WindowsAzure.StorageClient;
  2. using Microsoft.WindowsAzure;

Entity Class

We will have to define the Table structure

Entity Class

  1. /// <summary>
  2. /// Entity Class for the Storage Table
  3. /// </summary>
  4. public class REEntry : TableServiceEntity
  5. {
  6.     //Public properties for the column values
  7.     public string Alias { get; set; }
  8.     public string TopicTitle { get; set; }
  9.     public string Agenda { get; set; }
  10.     public string Level { get; set; }
  11.     public string DurationMinutes { get; set; }
  12.     public string PTCBio { get; set; }
  13.  
  14.     //Initializing the PartitionKey and RowKey values
  15.     public REEntry()
  16.     {
  17.         PartitionKey = DateTime.UtcNow.ToString("MMddyyyy");
  18.         RowKey = string.Format("{0,10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());

Context Class

Here we will be defining the Context.

Context Class

  1. /// <summary>
  2. /// Context Class to initialize the connection to the storage table
  3. /// </summary>
  4. public class REDataContext : TableServiceContext
  5. {
  6.     public REDataContext(
  7.         string baseAddress,
  8.         StorageCredentials credentials)
  9.         : base(baseAddress, credentials)
  10.     {}
  11.  
  12.     public IQueryable<REEntry> ReEntry
  13.     {
  14.         get { return this.CreateQuery<REEntry>("REEntry"); }
  15.     }
  16. }

DataSource Class

This will not only initialize the previous class but have those public methods by which we can add or access data from Azure Storage

DataSource Class

  1. /// <summary>
  2. /// Initializing the DataSource with public methods
  3. /// </summary>
  4. public class REDataSource
  5. {
  6.     private static CloudStorageAccount storageAccount;
  7.     private REDataContext context;
  8.  
  9.     static REDataSource()
  10.     {
  11.         storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnection");
  12.  
  13.         CloudTableClient.CreateTablesFromModel
  14.             (
  15.                 typeof(REDataContext),
  16.                 storageAccount.TableEndpoint.AbsoluteUri,
  17.                 storageAccount.Credentials
  18.             );
  19.     }
  20.  
  21.     public REDataSource()
  22.     {
  23.         context = new REDataContext(
  24.                 storageAccount.TableEndpoint.AbsoluteUri,
  25.                 storageAccount.Credentials);
  26.         context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
  27.     }
  28.  
  29.     //Getting the existing values
  30.     public IEnumerable<REEntry> REEntries
  31.     {
  32.         get
  33.         {
  34.             var results = from c in context.ReEntry
  35.                             where c.PartitionKey == DateTime.UtcNow.ToString("MMddyyyy")
  36.                             select c;
  37.             return results;
  38.         }
  39.     }
  40.  
  41.     //Adding new row to the table
  42.     public void AddREEntry(REEntry newREItem)
  43.     {
  44.         context.AddObject("REEntry", newREItem);
  45.         context.SaveChanges();
  46.     }
  47. }

Web Role

Global.asax

Global.asax

  1. void Application_Start(object sender, EventArgs e)
  2. {
  3.     CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetting) =>
  4.         {
  5.             configSetting(RoleEnvironment.GetConfigurationSettingValue(configName));
  6.         });
  7. }

Default.aspx

Default.aspx

  1. using System;
  2. using Microsoft.WindowsAzure;
  3. using ReadinessEventDBLibrary;
  4.  
  5. namespace Table_WebRole
  6. {
  7.     public partial class _Default : System.Web.UI.Page
  8.     {
  9.         public static bool storageInitialized = false;
  10.         public static object gate = new object();
  11.  
  12.         protected void btnSubmit_Click(object sender, EventArgs e)
  13.         {
  14.             InitializeStorage();
  15.  
  16.             //Initializing and adding an entry
  17.             REEntry entry = new REEntry() { Alias = txtAlias.Text, Agenda = "Agenda", DurationMinutes = "120", Level = "200", PTCBio = "Bio", TopicTitle = "Title" };
  18.             REDataSource ds = new REDataSource();
  19.             ds.AddREEntry(entry);
  20.             System.Diagnostics.Trace.TraceInformation("Partition Key : {0}, RowKey : {1}", entry.PartitionKey, entry.RowKey);
  21.  
  22.             txtAlias.Text = string.Empty;
  23.         }
  24.  
  25.         /// <summary>
  26.         /// Initializing the Storage
  27.         /// </summary>
  28.         private void InitializeStorage()
  29.         {
  30.             if (storageInitialized)
  31.                 return;
  32.  
  33.             lock (gate)
  34.             {
  35.                 if (storageInitialized)
  36.                     return;
  37.  
  38.                 try
  39.                 {
  40.                     var storage = CloudStorageAccount.FromConfigurationSetting("DataConnection");
  41.  
  42.                 }
  43.                 catch { }
  44.                 storageInitialized = true;
  45.             }
  46.         }
  47.     }
  48. }

Checking it out

Hit F5 to run the application but ensure that you are running Visual Studio 2010 in elevated mode.

image

Data can be visible from Server Explorer of Visual Studio 2010.

image

image

A very special thanks goes to Azure Training Kit. Download it today and learn Azure, nothing better than this. Enjoy Windows Azure and keep programming for Cloud!!!

Namoskar!!!