Windows AppFabric Caching for SessionState Provider ASP.NET

2:09:00 am 0 Comments

Using Windows Server AppFabric Caching for Asp.NET Session state provider.

In ASP.NET 4 we have following options to choose and use for managing session state,

AppFabric
1. In-Proc

2. SQL Server Mode

3. Custom


We can use custom session state mode to store session state in AppFabric cache.

To use AppFabric cache for session state we need to configure our application's web.config file according to steps mentioned below.

First of all make sure Windows Server AppFabric is installed and running.


Read install and configure windows server AppFabric to know how to install and configure windows server AppFabric.

Now add references to the following assemblies by right clicking on solution explorer and select add reference.

browse to AppFabric directory in windows/system32, select and add reference to both assemblies mentioned below.

1Microsoft.ApplicationServer.Caching.Client.dll
2Microsoft.ApplicationServer.Caching.Core.dll

Now open web.config file and add following in configSection

1<configsections>
2  <section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
3          Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0,
4          Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowlocation="true" allowdefinition="Everywhere">
5 
6</section></configsections>


Make sure to change your HOSTNAME in code below.

1<datacacheclient>
2  <!-- cache host(s) -->
3  <hosts>
4    <host name="AMIT" cacheport="22233">
5  </host></hosts>
6</datacacheclient>


Add this code inside system.web element in web.config

Make sure you write the correct cache name, i m using default cache.

1<sessionstate mode="Custom" customprovider="AppFabricCacheSessionStoreProvider">
2  <providers>
3    <add name="AppFabricCacheSessionStoreProvider" type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider" cachename="Default">
4  </add></providers>
5</sessionstate>


Now we can use AppFabric cache to store our session state as we used to do normally.

01protected void Page_Load(object sender, EventArgs e)
02{
03    // Just and example of storing data in session variables
04 
05    Session["Test"] = "AppFabricTest";
06 
07}
08 
09protected void btnTest_Click(object sender, EventArgs e)
10{
11     lblShow.Text = Session["Test"].ToString();
12}

0 comments: