Use for ASP.NET Sessions in Open Source
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 in Microsoft Visual Studio.
Step 2:
For NCache Open Source, install NuGet Package by executing the following command in the Package Manager Console inside Visual Studio:
Install-Package AspNet.SessionState.NCache.OpenSource
Step 3:
Verify Assembly References once this package is installed, please verify that references of the following assemblies have been added to your application.
Alachisoft.NCache.SessionStoreProvider.dll
Alachisoft.NCache.SessionStateManagement.dll
Step 4:
Please also verify that the 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.SessionStateNSessionStoreProvider"
sessionAppId="demoApp"
cacheName="democache" writeExceptionsToEventLog="false"
enableLogs="false"/>
</providers>
</sessionState>
Note
By default, the configuration section provided above has the cacheName
democache. This needs to be updated manually.
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 the following attributes according to your needs.
cacheName: This is the name of the cache you’ve created.
enableLogs: This turns error logging either on or off . If it is turned on, then NCache logs all errors in %NCHOME%\log-files\SessionStoreProvider. The value can be either true or false.
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 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: If this is set to true, then NCache logs errors to the Event log. However, please make sure that your ASP.NET user has permission to write to the Event Log. Otherwise, you’ll get errors when you run your application. The value can be either true or false.
exceptionsEnabled: If this is set to false, then NCache ignores exceptions thrown by the NCache server. If this is set to true, then NCache throws exceptions. The value can be either true or false.
Step 7:
Ensure that all objects in the session are serializable:
If you were previously using the InProc mode of ASP.NET Sessions, then the objects you were putting in 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. You just need to add the
[Serializable]
tag 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