Replacing ASP.NET Session with Velocity Session Provider

October 6, 2009

Replacing ASP.NET Session with Velocity Session Provider


One nice feature of
MicrosoftReplacing ASP.NET Session With Velocity Session Provider Distributed
Cache aka Velocity is
a custom session
provider
that can
replace the ASP.NET
default session provider.
In this post I’ll explain how to replace ASP.NET session with the
Velocity session provider that is being provided with Velocity.


Why Replacing the ASP.NET Session with Velocity Session?


Sometimes we want to share a session across servers in a server farm.
The ways to do so are to use a State Server or a database. When Velocity
came out it was released (currently in CTP) with a custom session provider.
The use of Velocity cluster as a session provider can have impact of
performance as opposed to the other methods since we are talking about
a distributed cache. Another reason to use Velocity as session provider is
the availability feature it gives us compared to State Server or database
which can fail and we will need a recovery plan for them.


Doing the Trick


First of all we need to reference the Velocity dlls from the ASP.NET
web site or web application. You should reference the CacheBaseLibrary
and ClientLibrary dlls. After we have the reference you should add the
following data in the web.config file –


In the configSections element add the following element:



<section name=”dataCacheClient” type=”Microsoft.Data.Caching.DataCacheClientSection, CacheBaseLibrary” 
allowLocation=”true” allowDefinition=”Everywhere”/>

Add the dataCacheClient element with your relevant configurations.
This can be for example:



<dataCacheClient deployment=”simple”>
  <localCache isEnabled=”true” sync=”TTLBased” ttlValue=”300″ />
  <hosts>      
    <host name=”localhost” cachePort=”22233″ cacheHostName=”DistributedCacheService”/>
  </hosts>
</dataCacheClient>

The last thing to do is in the system.web element to add the sessionState
element that will be custom and point to the Velocity session provider:



<sessionState mode=”Custom” customProvider=”Velocity”>
  <providers>
    <add name=”Velocity” type=”Microsoft.Data.Caching.DataCacheSessionStoreProvider, ClientLibrary” />
  </providers>
</sessionState>

That is it. After doing so you can run the Velocity cluster and use the
ASP.NET session as you used it before. The only difference will be that
the data will be saved inside Velocity.


A full example could look like:



<?xml version=”1.0″?>
<configuration>
  <configSections>
    <sectionGroup name=”system.web.extensions” type=”System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″>
      <sectionGroup name=”scripting” type=”System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″>
        <section name=”scriptResourceHandler” type=”System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ requirePermission=”false” allowDefinition=”MachineToApplication”/>
        <sectionGroup name=”webServices” type=”System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″>
          <section name=”jsonSerialization” type=”System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ requirePermission=”false” allowDefinition=”Everywhere” />         
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
    <section name=”dataCacheClient” type=”Microsoft.Data.Caching.DataCacheClientSection, CacheBaseLibrary” allowLocation=”true” allowDefinition=”Everywhere”/>
  </configSections>
 
  <dataCacheClient deployment=”simple”>
    <localCache isEnabled=”true” sync=”TTLBased” ttlValue=”300″ />
    <hosts>
      <host name=”localhost” cachePort=”22233″ cacheHostName=”DistributedCacheService”/>
    </hosts>
  </dataCacheClient>
 
  <appSettings/>
  <connectionStrings/>
 
  <system.web>
    <sessionState mode=”Custom” customProvider=”Velocity”>
      <providers>
        <add name=”Velocity” type=”Microsoft.Data.Caching.DataCacheSessionStoreProvider, ClientLibrary” />
      </providers>
    </sessionState>
  </system.web>
</configuration>

Summary


 


 


 


Lets sum up, I explained why you should consider using Velocity as a
session provider. The main reasons for that are performance and availability.
Also, I showed how to configure your ASP.NET application to use the
Velocity session provider.


DotNetKicks Image

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

5 comments

  1. http://ignou-student.blogspot.comNovember 26, 2009 ב 6:45

    I like this post very much, i will use this on my blog.

    Reply
  2. A VincentNovember 26, 2009 ב 18:34

    How about bringing this example up-to date with the Beta 1 release of AppFabric?

    I’ve tried and failed… the AppFabric(caching) documentation is once again pretty awful 🙁

    Reply
  3. Gil FinkNovember 27, 2009 ב 14:44

    @A Vincent,
    In AppFabric the only change is that you need to add a cacheName attribute to the add element like in the follwoing example:
    Reply

  4. Chris LoveNovember 27, 2009 ב 15:16

    Have they fixed it so it does not crash after a few seconds? I had a terrible time with it last summer crashing consistently after a few seconds. I was using WebDev because I will just testing it out, but still it should just work.

    Reply