Using Window Server AppFabric For Caching Your ASP.Net Session



The Window server AppFabric is generally available now and the latest version can be download from here. There is a number of features the Window server AppFabric provides to Monitoring, Hosting and managing the load of your application by providing scalability. But here I'm going to talk about the caching feature available in the latest version and using it in ASP.Net session management.

Why should I use Windows Server AppFabric for Session Management in ASP.Net?


Windows Server AppFabric provides a highly scalable in-memory application cache for all kinds of data. With distributed caching, your application can match increasing demand with increasing throughput by using a cache cluster that automatically manages the complexities of load balancing, scale-out, and failover. With the caching features of AppFabric you get:

  • Scalable in-memory, distributed cache for any serializable data.
  • Seamless integration with ASP.NET.
  • High availability and dynamic scale-out of cluster nodes.
  • Optional local cache with eviction policies.
  • Cache change subscriptions and notifications.

How to use ASP.Net Session provider?


Using the Custom SessionState setting to store the session into the Windows AppFabric Cache.

First add the following assembly reference to your project:
  • Microsoft.ApplicationServer.Caching.Core.dll
  • Microsoft.ApplicationServer.Caching.Client.dll

Now the question is; where to find these assembly files? You can locate these files in the %windir%\System32\AppFabric directory.

Add the following custom sessionstate settings


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

Now make sure your Cache service is configured and running properly and you have created the Cache named MySession. To install and configure the Cache Services read this article about Installing and configuring Windows Server AppFabric Cache Services.

You can now start using the session to store your data.

protected void btnGet_Click(object sender, EventArgs e)
{
txtItemValue.Text = (string)Session["Item1"];
}

protected void btnPut_Click(object sender, EventArgs e)
{
Session["Item1"] = txtItemValue.Text;
txtItemValue.Text = "";
}

Note:


Once you move the session state outside of the web server process, all the objects you put in the session need to be serializable.

Use the Serializable attribute on your classes to store the custom objects in Session.

Helpful? Just put your comments down below; I appreciate your inputs.