Use NCache for ASP.NET Sessions
NuGet is a popular way of simplifying the use of NCache in your .NET application. When you use NuGet to install the NCache package, it copies the library files to your Visual Studio solution and automatically updates your project (add references, change config files, etc.).
Step 1:
Create a new ASP.NET Web application (or ASP.NET Core Web Application) in Microsoft Visual Studio.
Step 2:
Install NCache NuGet Package for Professional Edition by executing the following command in Package Manager Console inside Visual Studio:
Install-Package AspNet.SessionState.NCache.Professional
Step 3:
Verify Assembly References once this package is installed, please verify that references of following assemblies have been added to your application.
- Alachisoft.NCache.SessionStoreProvider.dll
- Alachisoft.NCache.SessionStateManagement.dll
Step 4:
Please also verify that following section has been added to the Web.config of your ASP.NET application.
<sessionState cookieless="false"
regenerateExpiredSessionId="true"
mode="Custom"
customProvider="NCacheSessionProvider"
timeout="20">
<providers>
<add name="NCacheSessionProvider"
type="Alachisoft.NCache.Web.SessionState.NSessionStoreProvider"
sessionAppId="demoApp"
cacheName="demoCache" writeExceptionsToEventLog="false"
enableLogs="false"/>
</providers>
</sessionState>
Step 5:
Please note that timeout="20"
means that your sessions will expire after 20 minutes of inactivity. You can specify whatever value that suits you here.
Step 6:
With this configuration, all ASP.NET sessions of this application are stored in demoCache if it is configured and running. You can modify following attributes according to your needs.
CacheName: This is name of the cache you’ve created.
EnableLogs (“true” or “false”): This turns on or off error logging. If it is turned on, then NCache logs all errors in %NCHOME%\log-files\SessionStoreProvider.
SessionAppId: If you have multiple applications or app domains running on the same web farm and accessible from the same application, then you have the choice of either sharing your sessions across app domains or not. If you don’t want to share sessions across app domains, then specify a unique sessionAppId for each app domain. This ensures that each app domain puts its own sessionAppId to the SessionId thus making it impossible for the other app domains that might be using the same SessionId to fetch the same session.
WriteExceptionsToEventLog (“true” or “false”): If this is set to true, then NCache logs errors to the Event log. However, please make sure that your ASP.NET user-id has permission to write to Event Log. Otherwise, you’ll get errors when you run your application.
ExceptionsEnabled (“true” or “false”): If this is set to false, then NCache ignore exceptions thrown by NCache server. If this is set to true, then NCache throws exceptions upward.
Step 7:
Ensure all objects in session are serializable:
If you were previously using the InProc mode of ASP.NET Sessions, then the objects you were putting your Session did not need to be Serializable. However, if you were using StateServer or SqlServer modes for Sessions previously, then your objects are already serializable for all of this to work.
In either case, before using NCache for your sessions, you must ensure that all your custom objects that you’re putting in Session are serializable. It can be a very simple
[Serializable]
tag that you need to put on all your class definitions.
See Also
Create a Cache
Simulate Cache Usage
Monitor Caches in PerfMon
Use NCache from .NET Application
NCache Programmer's Guide