Gil Fink on .Net

Fink about IT

News

Microsoft MVP

MCPD Enterprise Applications Developer

Gil Fink

My Linkedin profile

Locations of visitors to this page

Creative Commons License

Blog Roll

Hebrew MSDN Articles

Index Pages

My OSS Projects

Replacing ASP.NET Session with Velocity Session Provider

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

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 6, 2009 10:17 AM

Replacing ASP.NET Session with Velocity Session Provider – Gil … | Webmaster Tools said:

Pingback from  Replacing ASP.NET Session with Velocity Session Provider &#8211; Gil &#8230; | Webmaster Tools

# October 6, 2009 12:02 PM

Replacing ASP.NET Session with Velocity Session Provider - Gil … said:

Pingback from  Replacing ASP.NET Session with Velocity Session Provider - Gil &#8230;

# October 6, 2009 1:23 PM

DotNetBurner - ASP.net said:

DotNetBurner - burning hot .net content

# October 7, 2009 1:28 AM

Reflective Perspective - Chris Alcock » The Morning Brew #449 said:

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #449

# October 7, 2009 9:35 AM

Twitter Trackbacks for Replacing ASP.NET Session with Velocity Session Provider - Gil Fink on .Net [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Replacing ASP.NET Session with Velocity Session Provider - Gil Fink on .Net         [microsoft.co.il]        on Topsy.com

# October 7, 2009 3:18 PM

9eFish said:

9efish.感谢你的文章 - Trackback from 9eFish

# October 7, 2009 5:06 PM

http://ignou-student.blogspot.com said:

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

# November 26, 2009 6:45 AM

rob said:

# November 26, 2009 10:06 AM

Replacing ASP.NET Session with Velocity Session Provider | Windows 7 Serials said:

Pingback from  Replacing ASP.NET Session with Velocity Session Provider | Windows 7 Serials

# November 26, 2009 11:18 AM

A Vincent said:

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 :(

# November 26, 2009 6:34 PM

Gil Fink said:

@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:

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

All the other things are the same.

# November 27, 2009 2:44 PM

Chris Love said:

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.

# November 27, 2009 3:16 PM

Use ???Velocity??? Session « Microsoft Technology, .Net, BizTalk, Sharepoint & etc. said:

Pingback from  Use ???Velocity??? Session &laquo; Microsoft Technology, .Net, BizTalk, Sharepoint &amp; etc.

# December 2, 2009 6:55 AM