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
Then after that in the Solution Explorer choose the project WebRole1 and right click to choose setting.
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.
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
- using Microsoft.WindowsAzure.StorageClient;
- using Microsoft.WindowsAzure;
Entity Class
We will have to define the Table structure
Entity Class
- /// <summary>
- /// Entity Class for the Storage Table
- /// </summary>
- public class REEntry : TableServiceEntity
- {
- //Public properties for the column values
- public string Alias { get; set; }
- public string TopicTitle { get; set; }
- public string Agenda { get; set; }
- public string Level { get; set; }
- public string DurationMinutes { get; set; }
- public string PTCBio { get; set; }
- //Initializing the PartitionKey and RowKey values
- public REEntry()
- {
- PartitionKey = DateTime.UtcNow.ToString("MMddyyyy");
- 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
- /// <summary>
- /// Context Class to initialize the connection to the storage table
- /// </summary>
- public class REDataContext : TableServiceContext
- {
- public REDataContext(
- string baseAddress,
- StorageCredentials credentials)
- : base(baseAddress, credentials)
- {}
- public IQueryable<REEntry> ReEntry
- {
- get { return this.CreateQuery<REEntry>("REEntry"); }
- }
- }
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
- /// <summary>
- /// Initializing the DataSource with public methods
- /// </summary>
- public class REDataSource
- {
- private static CloudStorageAccount storageAccount;
- private REDataContext context;
- static REDataSource()
- {
- storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnection");
- CloudTableClient.CreateTablesFromModel
- (
- typeof(REDataContext),
- storageAccount.TableEndpoint.AbsoluteUri,
- storageAccount.Credentials
- );
- }
- public REDataSource()
- {
- context = new REDataContext(
- storageAccount.TableEndpoint.AbsoluteUri,
- storageAccount.Credentials);
- context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
- }
- //Getting the existing values
- public IEnumerable<REEntry> REEntries
- {
- get
- {
- var results = from c in context.ReEntry
- where c.PartitionKey == DateTime.UtcNow.ToString("MMddyyyy")
- select c;
- return results;
- }
- }
- //Adding new row to the table
- public void AddREEntry(REEntry newREItem)
- {
- context.AddObject("REEntry", newREItem);
- context.SaveChanges();
- }
- }
Web Role
Global.asax
Global.asax
- void Application_Start(object sender, EventArgs e)
- {
- CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetting) =>
- {
- configSetting(RoleEnvironment.GetConfigurationSettingValue(configName));
- });
- }
Default.aspx
Default.aspx
- using System;
- using Microsoft.WindowsAzure;
- using ReadinessEventDBLibrary;
- namespace Table_WebRole
- {
- public partial class _Default : System.Web.UI.Page
- {
- public static bool storageInitialized = false;
- public static object gate = new object();
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- InitializeStorage();
- //Initializing and adding an entry
- REEntry entry = new REEntry() { Alias = txtAlias.Text, Agenda = "Agenda", DurationMinutes = "120", Level = "200", PTCBio = "Bio", TopicTitle = "Title" };
- REDataSource ds = new REDataSource();
- ds.AddREEntry(entry);
- System.Diagnostics.Trace.TraceInformation("Partition Key : {0}, RowKey : {1}", entry.PartitionKey, entry.RowKey);
- txtAlias.Text = string.Empty;
- }
- /// <summary>
- /// Initializing the Storage
- /// </summary>
- private void InitializeStorage()
- {
- if (storageInitialized)
- return;
- lock (gate)
- {
- if (storageInitialized)
- return;
- try
- {
- var storage = CloudStorageAccount.FromConfigurationSetting("DataConnection");
- }
- catch { }
- storageInitialized = true;
- }
- }
- }
- }
Checking it out
Hit F5 to run the application but ensure that you are running Visual Studio 2010 in elevated mode.
Data can be visible from Server Explorer of Visual Studio 2010.
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!!!
Comments
Anonymous
February 07, 2011
One very important thing you forgot to mention in your tutorial is that you have to set the Cloud storage account -something like CloudStorageAccount.SetConfigurationSettingPublisher( (configName, configSettingPublisher) => { var connectionString = RoleEnvironment.GetConfigurationSettingValue(configName); configSettingPublisher(connectionString); } ); You can do this on WebRole Startup , but as of Windows Azure SDK 1.3 you are advised to do this in Global.asax Application Startup . See reference here blogs.msdn.com/.../how-to-resolve-setconfigurationsettingpublisher-needs-to-be-called-before-fromconfigurationsetting-can-be-used-after-moving-to-windows-azure-sdk-1-3.aspxAnonymous
August 09, 2011
How to retrieve values from azure table-storage.Anonymous
August 18, 2011
To get the data from Storage you can use LINQ as below. var results = from c in context.ReEntry where c.PartitionKey == DateTime.UtcNow.ToString("MMddyyyy") select c;