NCache Session State Module allows you to transparently save and retrieve ASP.NET session-state objects within a webfarm. As compared to ASP.NET Session State Module which is centralized in nature, NCache Session State Module is truly distributed. It benefits from clustered caching and thus, it implicitly avoids single point of failure. Also, when running cache as InProc, no extra cost is involved in retrieving session-state objects.
Please follow these steps for enabling NCache Session State Module to transparently save and retrieve ASP.NET session-state objects.
<add
key = "CacheName"
value =
"myReplicatedCache"/>
<add
key = "sessionAppId"
value =
"1234"/>
<add
key = "exceptionsEnabled"
value =
"true"/>
<add
key = "enableLogs"
value =
"false" />
<add
key = "enableDetailLogs"
value =
"false"/>
<add
key = "clearASPSession"
value =
"false"/>
<add
key = "writeExceptionsToEventLog" value
= "false"/>
</appSettings>
Following is the description of different key-value pairs specified above:
Scenario 1:
If custom module is loaded before NCache's, custom module calls our Load function to load session from cache. In this case ncache will not load session information again. So it is custom http module responsibility to clear session before calling Load function.
Scenario 2:
If custom module is loaded after NCache's, clearAspSession should be set to false because ncache will clear session after updating the cache and custom module will get empty session. However custom module may have to clear session and then Load from cache again after ncache's module has already loaded session from cache.
<httpModules>
<add name = "NCacheWebSessionState" type = "Alachisoft.NCache.Web.SessionState.NSessionStateModule,
Alachisoft.NCache.SessionState, Version=3.8.X.X, Culture=neutral, PublicKeyToken=cff5926ed6a53769"/>
</httpModules>
<machineKey validationKey = "A01D6E0D1A5D2A22E0854CA612FE5C5EC4AECF24" decryptionKey = "ACD8EBF87C4C8937" validation = "SHA1"/>
If you make Session.Abandon() call, it does not delete the session from the cache in .NET 1.1. So, the preferred way of abandoning the session is by calling NSessionStateModule's Abandon call. However, you'll have to first get a handle to this NCache Session State Module from HttpApplication object. Here is the sample code:
using Alachisoft.NCache.Web.SessionState;
//you may write the following code under logout button
NSessionStateModule sm = Application["NSessionStateModule"] as NSessionStateModule;
sm.Abandon(Session);
The reason you have to do this is that if you simply call Session.Abandon(), there is no way ASP.NET provides for NSessionStateModule to find this out. Therefore, it cannot take the appropriate action.
Please note that this is not a problem in .NET 2.0 or later versions where you're able to use SSP.
NCache provides you an HttpModule (NSessionStateModule) to handle Session storage in the cache. This NSessionStateModul is called at AcquireRequestState
and ReleaseRequestState
events. However, if you've also developed an HttpModule and your event handlers are called before NSessionStateModule's event handlers are called, then your code is not likely to see the correct data in the Session object.
If your HttpModule is called before AcquireRequestState
event handler of NSessionStateModule is called, then you'll see an empty session. And, to handle this situation, you'll need to use the following code at the beginning of your HttpModule's event handler:
using Alachisoft.NCache.Web.SessionState;
//you may write the following code under logout button
NSessionStateModule sm = Application["NSessionStateModule"] as NSessionStateModule;
sm.Load(HttpContext);
If your code is making changes to the Session object and wants to make sure these changes are seen by other ASP.NET pages, then you should call NSessionStateModule.Update(HttpContext)
in the following way:
using Alachisoft.NCache.Web.SessionState;
//you may write the following code under logout button
NSessionStateModule sm = Application["NSessionStateModule"] as NSessionStateModule;
sm.Update(HttpContext);
This will ensure that everything is working properly.
Please note that if you're using Session.Clear(), NCache will remove this Session from the cache. Upon trying to save the Session content, NCache checks to see if Session is empty (Session.Count == 0). If that is the case, then it calls a cache.Remove(…).
Using Session State Store Provider | Using sessions in geographically separated web farms