Wednesday, December 21, 2011

How to maintain session state in Window Azure

In ASP.NET there is concept of a session which is maintained from the user’s first request to their last request for visit of the web site. By default, ASP.NET session is maintained in the RAM of the running web server.

However Window Azure is statless!, web role haven't any local storage.

If only one instance in webrole then usaul session will work but when you make more then one instance then sesion will not work because at any time trafic will be moved to different instance the data center.

generally in web.config session state defined as below

<sessionState timeout="20" mode="InProc"></sessionState>

that is InProc session state maintain in process and will remove after 20 minutes

In Window azure you can maitain session state by following ways

1) By SQL Azure

you can use sql azure database to store session values.

web.config Change

<sessionState mode="SQLServer"
sqlConnectionString="connectionstring"
cookieless="false"
timeout="20"
allowCustomSqlDatabase="true"
/>

Example


2) By Table Storage

you can use storage (table storage) availbale on window azure 

web.config Change

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

Example


Download aspprovider from here

3) By AppFabric Caching

You can use appfbric  caching to store session values

web.config Change

<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
<providers>
<!-- specify the named cache for session data -->
<add
name="AppFabricCacheSessionStoreProvider"
type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider"
cacheName="NamedCache1"
sharedId="SharedApp"/>
</providers>
</sessionState>

Example


No comments: