SQL Azure for session storage

To give myself some deployment options for the Whisky Fringe 2012 Tasting Tracker site I’ve been using my Microsoft Azure 90 day trial account to try setting up cloud-based hosting. This comprises two services:

  • A Cloud Service to which the MVC 4 web project is deployed
  • A SQL Azure database

Given that I might want to scale up the site for the event session state needs to be managed by the SQL Azure database, or an Azure table. Scott Hanselman posted about how to use the System.Web.Providers NuGet package to handle this, and gave sample configuration. For some reason, while the membership, roles and profiles parts of this worked just fine it refused to set up the relevant Sessions table as was expected – firing up the app lead to a yellow screen of death with an error:

Invalid object name ‘dbo.Sessions’.

After wasting a bit of time figuring out why it wasn’t creating the table, I resorted to letting it create the table against my local SQL Express instance (which worked without incident) then scripting directly to the SQL Azure instance:

CREATE TABLE [dbo].[Sessions](
    [SessionId] [nvarchar](88) NOT NULL,
    [Created] [datetime] NOT NULL,
    [Expires] [datetime] NOT NULL,
    [LockDate] [datetime] NOT NULL,
    [LockCookie] [int] NOT NULL,
    [Locked] [bit] NOT NULL,
    [SessionItem] [image] NULL,
    [Flags] [int] NOT NULL,
    [Timeout] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [SessionId] ASC
))
GO

Huzzah! Working sessions. The only caveat was that SQL Azure has limitations on the kinds of DDL operations you can use, so when you script from your local instance you’ll find the CREATE TABLE from above also has:

WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

tagged onto the end – omitting that’ll get it scripting into Azure just fine.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.