Windows AppFabric Caching for SessionState Provider ASP.NET

Using Windows Server AppFabric Caching Session State Provider For Asp.Net 3.5 And 4.0. In ASP.NET 4 we have following options to choose and use for managing session state,

Windows Server AppFabric Caching Session State Provider
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.

Microsoft.ApplicationServer.Caching.Client.dll 
Microsoft.ApplicationServer.Caching.Core.dll

Now open web.config file and add following in configSection


  


Make sure to change your HOSTNAME in code below.


  
  
    
  



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

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


  
    
  



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

protected void Page_Load(object sender, EventArgs e)
{
    // Just and example of storing data in session variables

    Session["Test"] = "AppFabricTest";
   
}

protected void btnTest_Click(object sender, EventArgs e)
{
     lblShow.Text = Session["Test"].ToString(); 
}

If you like this post than join us or share

1 comments:

vijay said...

Hi Amit,

I have created an ASP .net web application and incorporated necessary changes as suggested in the above post for both Caching and Session Management.
Caching works fine. Coming to Session management, it is working fine when try to create a cache and retrieve in my local. But when i try to retrieve the same session from different machine, unable to get the session value (null exception). According to my knowledge, AppFabric Session management is Outproc. If so why am i not able to get the same session value from different machine? Please help me out.


Find More Articles