Thursday, June 4, 2009

Windows Azure - TableNotFound

I got this error while trying out Windows Azure after deploying to the cloud fabrik. When testing locally we use the option shown on the picture bellow but on the cloud that is not required.

AzureCreateTestTablesMenu

My problem was that I was not calling the initialization code that creates the tables in the storage. This is not required locally. The recommend initialization for the cloud is demonstrated in this video:

Store Data in Windows Azure Tables

The initialization is done in the Global.axax in the BeginRequest event handler. My looks like this:

   1: protected void Application_BeginRequest(object sender, EventArgs e)
   2: {
   3:     HttpApplication app = (HttpApplication)sender;
   4:     HttpContext context = app.Context;
   5:     FirstRequestInitialization.Initialize(context);
   6: }

And the FirstRequestInitialization class looks like this:

   1: class FirstRequestInitialization
   2: {
   3:     private static bool s_InitializedAlready = false;
   4:     private static Object s_lock = new Object();
   5:  
   6:     public static void Initialize(HttpContext context)
   7:     {
   8:         if (s_InitializedAlready)
   9:         {
  10:             return;
  11:         }
  12:         lock (s_lock)
  13:         {
  14:             if (s_InitializedAlready)
  15:             {
  16:                 return;
  17:             }
  18:  
  19:             ApplicationStartUponFirstRequest(context);
  20:             s_InitializedAlready = true;
  21:         }
  22:     }
  23:  
  24:     private static void ApplicationStartUponFirstRequest(HttpContext context)
  25:     {
  26:         StorageAccountInfo accountInfo =
  27:             StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();
  28:         
  29:         TableStorage.CreateTablesFromModel(typeof(BrandTable), accountInfo);
  30:         // other tables here...
  31:     }
  32: }

Basically what this code does is to read the configuration from the configuration file and create each required table. If you get this error and you are using this method you forgot to add a call to the CreateTablesFromModel with the missing table.

No comments: