TopMenu

Using Window server AppFabric for Caching Asp.net session


The Window server AppFabric is generally available now and you can download the latest version from here. There’s no. of features the Window server AppFabric provides to Monitoring, Hosting and managing the load of your application by providing scalability. But here I’m gonna 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 below assembly reference to your project:
·         Microsoft.ApplicationServer.Caching.Core.dll
·         Microsoft.ApplicationServer.Caching.Client.dll
Now the question Where to find these assembly files? You can locate these files in %windir%\System32\AppFabric directory.
Add dataCacheClient settings in config section of your configuration file

<section name="dataCacheClient"
          type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
            Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0,
            Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          allowLocation="true"
          allowDefinition="Everywhere"/>
 
Now outside the config section define the dataCacheClient setting

    <dataCacheClient>
    <!-- (optional) specify local cache -->
    <localCache
      isEnabled="false"
      sync="TimeoutBased"
      objectCount="100000"
      ttlValue="300" />

    <!-- cache host(s) losthost for local cache or mention the name of cacheHost -->
    <hosts>
      <host
         name="localhost"
         cachePort="22233" />
    </hosts>

    <!-- Security is disabled, otherwise Grant-CacheAllowedClientAccount cmdlet must be used to authorize appPool Identity or You can disabled the security using Set-CacheClusterSecurity cmdlet set all none. But you need to take down the CacheCluster and change and restart again-->
    <securityProperties mode="None" protectionLevel="None" />

  </dataCacheClient>

Add the below 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 Post 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 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.

1 comment:

  1. what will happen when Session.Abandon() calls and how to trigger Session_OnEnd kind of event.

    ReplyDelete