Use for ASP.NET Core Sessions
NuGet is a popular way of simplifying the use of NCache in your .NET Core 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 Core application in Microsoft Visual Studio.
Step 2:
- 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 AspNetCore.Session.NCache
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
- Alachisoft.NCache.CoreSessionStoreProvider.dll
Step 4:
Please also verify that following section has been added to the AppSettings.json of your ASP.NET Core application.
{
"AppSettings": {
"SiteTitle": "ASP.NET MVC Music Store",
"CacheDbResults": true,
},
"NCacheSessions": {
"SessionAppId": "demoApp",
"SessionOptions": {
"CookieName": "AspNetCore.Session",
"CookieDomain": null,
"CookiePath": "/",
"CookieHttpOnly": "True",
"IdleTimeout": "5",
"CookieSecure": "None"
},
"CacheName": "demoClusteredCache",
"EnableLogs": "True",
"RequestTimeout": "90"
},
}
Please note that idletimeout="5"
means that your sessions will expire after 5 minutes of inactivity. You can specify whatever value that suits you here.
With this configuration, all ASP.NET Core 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 for windows and opt/ncache/log-files/SessionStoreProvider for Linux.
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.
Step 5:
NCache provides the AddNCacheSession() method to initialize configurations from external files before adding the session services to the container. Using this method, you can refer to the configurations by providing the name of the section containing JSON format configurations in Appsettings.json:
public void ConfigureServices(IServiceCollection services)
{
. . .
//Add services to the container
services.AddNCacheSession(Configuration.GetSection("NCacheSessions"));
. . .
}
Step 6:
Once the services have been initialized, you can now configure the HTTP request pipeline by adding middleware in the Configure()
method. The following is an excerpt showing the addition of a middleware layer through the use of UseNCacheSession()
extension method in IApplicationBuilder
. The NCache session middleware should always be stacked before the layer that utilizes the sessions.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseNCacheSession(); //store NCache session data
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=myApp}/{action=Index}/{id?}");
});
}
Step 7:
Ensure all objects in session are serializable:
If you were previously using the InProc mode of ASP.NET Core 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