Hi Again,
After a long break I'm posting something new on my blog. This time something related to Windows Azure development.
This post is about how we can
manage a session in Windows Azure. As we know that there are different ways of
session management in ASP.NET like:
In Proc
Out Proc (SQL Server and State
Server)
Custom
So here I've used a Custom mode to
store the session in Windows Azure which is called Table Storage. Below are the steps which allow us to store
our session on Azure storage account using Table Storage method:
1) We have to download the code for Table Storage provider
developed by Windows Azure team from the below URL:
2) Compile
the project downloaded from the above URL and add the dll as a reference in
your project.
3) In the web.config of the project, add the following setting
in the< sessionState> section:
<sessionState mode="Custom" customProvider="TableStorageSessionStateProvider">
<providers>
<clear/>
<add name="TableStorageSessionStateProvider" type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider" />
</providers>
</sessionState>
4) Go to the web role properties and
add the following setting in the ServiceConfiguration.csfg file:
<ConfigurationSettings>
<Setting name="DataConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<Azure Storage
Account Name>;AccountKey=<Azure Storage Account Key> " />
</ConfigurationSettings>
You can add this setting in the
web.config or app.config file of your application also:
<appSettings>
<add key="DataConnectionString"value="DefaultEndpointsProtocol=https;AccountName=<Azure
Storage Account Name>;AccountKey=<Azure Storage Account Key>
" />
</appSettings>
5) In the Global.asax file of the
project, get the CloudStorageAccount details before accessing the table
storage. Write the following code to achieve this:
usingMicrosoft.WindowsAzure;
usingMicrosoft.WindowsAzure.ServiceRuntime;
usingSystem.Configuration;
protected void Application_Start()
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName,
configSettingPublisher) => { varconnectionString
= RoleEnvironment.IsAvailable ? RoleEnvironment.GetConfigurationSettingValue(configName)
: ConfigurationManager.AppSettings[configName];
configSettingPublisher(connectionString); });
}
In the above code, if you have not
specified the “DataConnectionString” in the Role configuration, it will try to
look up in the web.config or app.config of your application. So make sure, the
“DataConnectionString” must be present in either of these two places.
6)
Connect to Server Explorer and in
the Windows Azure Storage section, add the new storage account with the Account
Name and Key provided in the step 4. You will be able to see the Session table
in the Azure.