Replacing ASP.NET Session with Velocity Session Provider
Replacing ASP.NET Session with Velocity Session Provider
One nice feature of
Microsoft
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.