Introduction
This article is for developers who are planning to move their MVC application to an Azure environment. This will also work as an alternative of default SQL Server session state in an MVC application.
Why we need a new approach
To host application in Azure, we can't go with default Inproc and StateServer. There are a few options available in Azure for the same.
- Table Storage (We can also go with this approach but it will need extra cost)
- Windows Azure Caching (This is the fastest one but it is very costly)
- SQL Azure
I deployed the application with SQL Server mode. It was working fine on local servers but now it is giving me an error.
It needs ASP.NET version 2.0 session state on Azure environment or it needs some work around on it. Also, it needs a separate service (web job) to clear the expired sessions.
But, I found a better solution for it.
Solution
So, after searching, I planned to use
ASP.NET Universal Providers. You can install it through the NuGet Package Manager. It is very easy to use. It provides other features as well, but I will only focus on the session state.
Steps to follow
So, you need to follow the below steps in order to implement ASP.NET Universal Providers in your MVC application. I am assuming, you already have a working MVC application.
Open Package Manager Console.
In Visual Studio, go to View -> Other Windows -> Package Manager console
Enter the following command in the command window.
- Install-Package System.Web.Providers -Version 1.2.0
It will install 3 packages in your application.
- System.Web.Providers.1.2.0
- Microsoft.AspNet.Providers.Core.1.0.0
- Microsoft.AspNet.Providers.1.1.0
Go to web.config
In web.config, you will see that the following highlighted lines have been added.
- <connectionStrings>
- <add name="YouApplicationConnectionString" connectionString="yourapplicationconnectionstring" providerName="System.Data.SqlClient" />
- <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-AzureSampleApp-20170614050859;Integrated Security=SSPI" />
- </connectionStrings>
And, this code is added inside <system.web> tag.
- <profile defaultProvider="DefaultProfileProvider">
- <providers>
- <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
- </providers>
- </profile>
- <membership defaultProvider="DefaultMembershipProvider">
- <providers>
- <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
- </providers>
- </membership>
- <roleManager defaultProvider="DefaultRoleProvider">
- <providers>
- <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
- </providers>
- </roleManager>
- <!--
- If you are deploying to a cloud environment that has multiple web server instances,
- you should change session state mode from "InProc" to "Custom". In addition,
- change the connection string named "DefaultConnection" to connect to an instance
- of SQL Server (including SQL Azure and SQL Compact) instead of to SQL Server Express.
- -->
- <sessionState mode="InProc" customProvider="DefaultSessionProvider">
- <providers>
- <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
- </providers>
- </sessionState>
Changes required in web.config file
Inside <system.web>, remove all the newly added tags except <sessionState> and update this tag as -
- <sessionState mode="Custom" customProvider="DefaultSessionProvider" timeout="30" >
- <providers>
- <clear/>
- <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="SQLAzureSession" />
- </providers>
- </sessionState>
Also, update the <connectionstrings> tag.
- <connectionStrings>
- <add name="YouApplicationConnectionString" connectionString="yourapplicationconnectionstring" providerName="System.Data.SqlClient" />
- <add name="SQLAzureSession" providerName="System.Data.SqlClient" connectionString="yourazuredatabaseconnectionstring" />
- </connectionStrings>
You can use both the connection strings as same or use only one connection string. This Provider creates only 1 table with the name "Sessions" in database.
That's it. You don't need to do anything else. It will automatically create a new table in the database and also remove expired sessions automatically. You need at least one session object to be declared in your application, to generate session row in the database.
Summary
Using ASP.NET Universal Providers custom session provider in place of default SQL Server option is an easier and good approach, especially when you are working with Azure.