Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Custom Session State Management

Is it possible to build a fully customized Session State Mode instead of using Inproc or SQLServer?

like image 862
Saber Shebly Avatar asked Mar 04 '11 14:03

Saber Shebly


2 Answers

Yes, absolutely, you could write a custom session state store provider by implementing SessionStateStoreProviderBase class and then set the session state mode to this custom implementation in web.config:

<sessionState 
    mode="Custom"
    customProvider="SomeCustomProviderName">
    <providers>
        <add name="SomeCustomProviderName"
             type="YourNamespace.CustomSessionStateStore"
             connectionStringName="SomeConnectionString" />
    </providers>
</sessionState>
like image 164
Darin Dimitrov Avatar answered Sep 30 '22 23:09

Darin Dimitrov


This is an older question relating to something I'm doing in the here and now of Oct 2014.

Basically, the original answers are still correct, in that you can build your own custom OutProc session state management system but with Microsoft now being much more involved with open source, you could just as easily trip along to MSOpenTech and download the Redis port for Windows which is also supported by Windows Azure.

Redis is an OutProc session state management system that can be run in a stand alone process/console window or as windows service for backplane session/ key value store.

MSOpenTech:

Redis is a very popular open-source, networked, in-memory, key-value data store. It is known for high performance, flexibility, a rich set of data structures, and a simple straightforward API. MS Open Tech has been working with the Redis community to build a production-ready Windows port of Redis, including 64-bit support, an installer for Windows Azure, NuGet support, and much more.

Redis.io Intro:

Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs

You can use it to store session state for ASP.NET MVC, WebAPi and SignalR which is super handy in a web farm environment.

Install-Package Microsoft.Web.RedisSessionStateProvider

It's easily configured via web.config and when I say easy, I mean easy.

<system.web>
  <sessionState mode="Custom" customProvider="MySessionStateStore">
      <providers>
        <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="127.0.0.1" port="6379" accessKey="" ssl="false" />
      </providers>
    </sessionState>
</system.web>

So easy in fact I at first thought I must be doing it wrong. ;) But that's all it takes to farm out session state to Redis.

Its a port with high performance, very close to the original POSIX version.

We are using it for a financial services app which encounters high loads of realtime pushes and pulls via Signalr, which as above also "supports" Redis for maintaining hub session state.

So while you can still do as the accepted answers, some 3 and a half years later I would look towards some of these alternatives to the default ASP.NET session management experience which are available, for free, via OSS.

https://github.com/MSOpenTech/redis

http://www.codeproject.com/Articles/636730/Distributed-Caching-using-Redis

like image 35
rism Avatar answered Sep 30 '22 23:09

rism