Saturday, November 22, 2008

Azure Storage Services Test Harness: Table Services 7 – Testing for Table Existence at App Startup Only

Update 1/31/2009: Code is now available for download.

This is the first of a series of articles about a ASP.NET 3.5 C# test harness for Azure Storage Services that is will be available for download from my “Retire Your Data Center” cover story for Visual Studio Magazine’s February 2009 issue (click the Get Code Download link) in the near future.

Most Azure Table Services sample projects invoke the TableStorage.CreateTablesFromModel() method before each attempt to perform CRUD operations in a user’s session. This method creates the named table if it doesn’t exist or sends a 409 “The table specified already exists” response message if it does. This is a relatively expensive process, especially if repeated before each CRUD request to the service.

Steve Marx raised this issue in his Try to Create Tables Only Once post of 11/18/2008.

This article describes the test harness’s code in the Application_BeginRequest event handler of the Global.asax.cs file to test for tables only at the start of the user’s session.

Preceding episodes of this Azure Storage Services Test Harness series are:

The HTTP POST Request and Response Headers for the CreateTablesFromModel() Method

POST Request and HTTP Header (from Table Services 1 – Introduction and Overview):

POST http://oakleaf.table.core.windows.net/Tables
POST /Tables HTTP/1.1
User-Agent: Microsoft ADO.NET Data Services
x-ms-date: Mon, 10 Nov 2008 16:06:02 GMT
Authorization: SharedKeyLite oakleaf:eRbDw5U7BkeSfZmKj71Zy3WTjrZCWkseVav3NK3tVqA=
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
DataServiceVersion: 1.0;NetFx
MaxDataServiceVersion: 1.0;NetFx
Content-Type: application/atom+xml
Host: oakleaf.table.core.windows.net
Content-Length: 499
Expect: 100-continue
Proxy-Connection: Keep-Alive

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
    xmlns="http://www.w3.org/2005/Atom">
  <title />
  <updated>2008-11-10T16:06:02.1557288Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:TableName>CustomerTable</d:TableName>
    </m:properties>
  </content>
</entry>

Notes: The maximum skew between the x-ms-date value and server UTC is 15 minutes. Blob Storage, Table Storage, and Queue support the SharedKey authentication scheme. The key signature is a Hash Message Authentication Code (HMAC) constructed from the request and computed with the SHA256 algorithm, then encoded using Base64 encoding. The ADO.NET Data Services’ .NET Client library (System.Data.Services.Client) supports a simpler SharedKeyLite authentication scheme only.

POST Response Header and Message for Existing Table

If the table doesn’t exist, the project creates an empty table. If the table exists, the POST attempt sends the following error response:

HTTP/1.1 409 The table specified already exists.
Cache-Control: no-cache
Content-Length: 258
Content-Type: application/xml
Server: Table Service Version 1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: 7b66eaa7-68f4-4926-ae43-579074b71c04
Date: Mon, 10 Nov 2008 16:05:50 GMT

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code>TableAlreadyExists</code>
  <message xml:lang="en-US">The table specified already exists.</message>
</error>

Code to Execute TableStorage.CreateTablesFromModel() Once per User Session

Ordinarily, you would expect to write code in the Global.asax.cs files’ Application_Start event handler for a once-per session initialization operation, but that doesn’t work for IIS 7. The test harness code code is based on Mike Volodarsky’s IIS7 Integrated mode: Request is not available in this context exception in Application_Start post of 11/10/2007, which notes that IIS 7 will throw an error if you attempt to execute code from Application_Start in IIS 7’s Integrated mode.

ASP.NET Web Cloud Services don’t add a Global.asax.cs file by default, so you must add one to the ProjectName_WebRole project by selecting the Global Application Class in the Web category of the Add New Item dialog. The added Global.asax.cs file includes event-handling stubs for commonly used application-level events, including Application_BeginRequest.

Here’s the test harness’s Application_BeginRequest code":

0 comments: