MICHELVANDERVLUGT.NET RSS 2.0
 Friday, August 20, 2010

Lately I’ve been playing around with Microsoft Azure (for now in the offline development environment only) and it is really easy to get started. However, as easy it is to get your ASP.NET app running, it is a bit more difficult to get access to the storage part (blobs, table storage and queues). There is no straight forward how-to available, at least I couldn’t find one. So I’ve combined some of the resources I’ve found (e.g. the example from Windows Azure Table Storage – Not Your Father’s Database, by Julie Lerman and the PDC09 presentation Windows Azure Tables and Queues Deep Dive, by Jai Haridas) and here it is…

Step 1
Create a new ‘Windows Azure Cloud Service’ project and name it something like CloudServiceDemo, hit ok. Then select an ‘ASP.NET Web Role’ to be included in your cloud service solution and again hit Ok. Now two projects are created, one with roles and configuration and one with the actual ASP.NET web site.

When you run this the ‘Windows Azure Simulation Environment’ starts and your web application appears in your browser (note: at this moment my Queue storage fails with an error that it is being used by another process, something which I haven’t figured out yet).

Step 2
Change two configuration files:

  1. In the ServiceDefinition.csdef, add line 3 in the configuration settings:
       1:  <ConfigurationSettings>
       2:    <Setting name="DiagnosticsConnectionString" />
       3:    <Setting name="DataConnectionString" />
       4:  </ConfigurationSettings>
  2. In the ServiceConfiguration.cscfg, add line 3 in the configuration settings:
       1:  <ConfigurationSettings>
       2:    <Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />
       3:    <Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
       4:  </ConfigurationSettings>

Step 3
Add the following code to the OnStart method of the WebRole.cs

   1:  // Setup a handler to update CloudStorageAccount instances when the
   2:  // configuration settings change in the service configuration file.
   3:  CloudStorageAccount.SetConfigurationSettingPublisher(
   4:      (configName, configSetter) =>
   5:      {
   6:          // Provide the configSetter with the initial value
   7:          configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
   8:   
   9:          RoleEnvironment.Changed +=
  10:              (sender, arg) =>
  11:              {
  12:                  if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()
  13:                      .Any((change) => (change.ConfigurationSettingName == configName)))
  14:                  {
  15:                      // The corresponding configuration setting has changed, propagate the value
  16:                      if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
  17:                      {
  18:                          // In this case, the change to the storage account credentials in the
  19:                          // service configuration is significant enough that the role needs to be
  20:                          // recycled in order to use the latest settings. (for example, the 
  21:                          // endpoint has changed)
  22:                          RoleEnvironment.RequestRecycle();
  23:                      }
  24:                  }
  25:              };
  26:      }
  27:  );

Step 4
Create a class that represents the data in the table storage (you need to add a reference to System.Data.Services.Client)

   1:  /// <summary>
   2:  /// Representation of a movie
   3:  /// </summary>
   4:  /// <remarks>PartitionKey, RowKey and TimeStamp are required for Azure table storage.</remarks>
   5:  [DataServiceKey("PartitionKey", "RowKey")]
   6:  public class Movie
   7:  {
   8:      /// <summary>
   9:      /// Category of the movie, e.g. Action, SciFi, etc.
  10:      /// </summary>
  11:      public string PartitionKey { get; set; }
  12:   
  13:      /// <summary>
  14:      /// Title of the movie
  15:      /// </summary>
  16:      public string RowKey { get; set; }
  17:   
  18:      public DateTime TimeStamp { get; set; }
  19:   
  20:      public int ReleaseYear { get; set; }
  21:  }

Step 5
Now you can create the ‘Movie’ table using a CloudStorageAccount

   1:  CloudStorageAccount storageAccount =
   2:      CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
   3:   
   4:  storageAccount.CreateCloudTableClient().CreateTableIfNotExist("Movies");

Step 6
And access the storage using a TableServiceContext

   1:  // Create a context
   2:  TableServiceContext context = 
   3:      new TableServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);
   4:   
   5:  // Add movie
   6:  Movie newMovie = 
   7:      new Movie { PartitionKey = "Action", RowKey = "An action movie", ReleaseYear = 2005 };
   8:  context.AddObject("Movies", newMovie);
   9:  context.SaveChanges();
  10:              
  11:  // Get movies
  12:  IEnumerable<Movie> movies = from m in context.CreateQuery<Movie>("Movies")
  13:                              where m.PartitionKey == "Action"
  14:                              select m;

Of course this is just a start, now the code has to be placed into specific classes dividing the responsibilities. Something like a StorageContext (which inherits from TableServiceContext) to encapsulate the tablename and the context.CreateQuery, and a MovieRepository to encapsulate the context and the get-, add- and update-methods…

Update: see Windows Azure Table Storage in the development environment, part II for an example.

Friday, August 20, 2010 12:36:47 AM (W. Europe Standard Time, UTC+01:00)  #    Comments [8] -

Archive
<May 2013>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2013
Michel van der Vlugt
Sign In
All Content © 2013, Michel van der Vlugt
DasBlog theme 'Business' created by Christoph De Baene (delarou)