Use 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.).
Create a new ASP.NET application (or ASP.NET Core) in Microsoft Visual Studio.
- For NCache Enterprise Edition, install NCache NuGet Package for Enterprise Edition by executing the following command in Package Manager Console inside Visual Studio:
Install-Package Alachisoft.NCache.Enterprise.SessionStateProvider -Version 4.9.0
Verify Assembly References once this package is installed, please verify that references of following assemblies have been added to your application.
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" stimeout="20"> <providers> <add name="NCacheSessionProvider" type="Alachisoft.NCache.Web.SessionState.NSessionStoreProvider" sessionAppId="myApp" cacheName="demoClusteredCache" writeExceptionsToEventLog="false" enableLogs="false"/> </providers> </sessionState>
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.
With this configuration, all ASP.NET sessions of this application are stored in demoClusteredCache 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.
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.