Managing .NET application session state in Azure Cloud

  • Home
  • Blogs
  • Managing .NET application session state in Azure Cloud

Managing .NET application session state in Azure Cloud

About CloudSight

Session is a State Management Technique. A Session can store the value on the Server. It can support any type of object to be stored along with our own custom objects..

In general, the ways which are used in maintaining the Session State in the .NET application are

  • In-Proc, which stores session state in the individual web server’s memory. This is the default option.
  • Out-Proc / State Server, which stores session state in another process, called ASP.NET state service. It can also be run on a separate server.
  • SQL Server, which stores session state in a SQL Server database.

So, what are the options to maintain Session State in Azure Cloud?.
Table Storage
Table Storage Session Provider is, enabling developers to store session state in Azure Table Storage. It actually works by storing each session as a record in Table Storage. Each record will have an expired column that will have the expiry time of each session if it is not in use for certain time. The advantage of Table Storage Session Provider is its relatively low cost.
The disadvantage of Table Storage Session Provider is that it may not perform as fast as the other options.
We have to apply the following code snippet in web.config when using Table Storage Session Provider:

<sessionState mode="Custom" customProvider="TableStorageSessionStateProvider"> 
    <providers> <clear/>
    <add name="TableStorageSessionStateProvider" 
    type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider" />
    </providers>
    </sessionState>


SQL Azure
SQL Azure session management is almost similar to SQL Server Session Provider. It can be used as storage for session state, it is cost effective, especially when you have an existing SQL Azure database, it performs better than Table Storage Session Provider in most cases. Expired session needs to be cleaned up by calling the DeleteExpiredSessions stored procedure.
We have to apply the following code snippet in web.config when using SQL Azure Session Provider:
<sessionState mode="SQLServer" 
    sqlConnectionString="Server=tcp:serverName].database.windows.net; 
    Database=myDataBase;User ID=[LoginForDb]@[serverName];
    Password=[password];Trusted_Connection=False; Encrypt=True;" 
    cookieless="false" timeout="20" allowCustomSqlDatabase="true" />


Azure Caching
The most preferable option available since it provides a high-performance, in-memory, distributed caching service. It is an out-of-process storage mechanism for ASP.NET applications.
We have to apply the following code snippet in web.config when using Cache Session Provider:

<sessionState mode="Custom" 
    customProvider="AzureCacheSessionStoreProvider"> 
    <providers>
      <add name="AzureCacheSessionStoreProvider" 
    type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, 
    Microsoft.Web.DistributedCache" cacheName="default" useBlobMode="true" 
    dataCacheClientName="default" />
        </providers>
    </sessionState>

a. Azure Cache for Redis
Redis is another option for maintaining the session state on cloud. It is a fully managed, open source–compatible in-memory data store to power fast, scalable applications.
We have to apply the following code snippet in web.config when using Redis Cache Session Provider:
<sessionState mode="Custom" customProvider="MySessionStateStore">
        <providers>
    <!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki -->       
    <!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
    <add 
    name="MySessionStateStore"type="Microsoft.Web.Redis.RedisSessionStateProvider" 
    host="hecccl.redis.cache.windows.net" accessKey="" 
    connectionString="hecccl.redis.cache.windows.net:6380,
    password=[password],ssl=True,abortConnect=False" ssl="true" port="6380" 
    throwOnError="true" retryTimeoutInMilliseconds="5000" 
    applicationName="CCLLogin" />
    </providers>   
    </sessionState>


Conclusion
I have discussed above some of the available options for managing session state in Windows Azure. Azure Caching is the recommended option.
For more information, please write to us at info@cloudsight.co.in.