ASP.NET Core Session Provider
ASP.NET Core Session contains temporary user data while the user browses the application. The session is kept in an in-memory store to persist data across multiple web requests from a client. The session data is considered temporary and any permanent data must also be stored in the database.
For ASP.NET Core applications running in multi-server environments, you can use NCache as the ASP.NET Core Session provider. NCache provides an extremely fast and scalable in-memory ASP.NET Core Session storage with replication for high availability.
Configure ASP.NET Core Session Provider
Here is the code that configures ASP.NET Core to use NCache as its Session provider.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddControllersWithViews();
// Set up MVC Services
builder.Services.AddMvc();
// Configure NCache Session State
builder.Services.AddNCacheSession(options =>
{
options.CacheName = "demoCache";
options.EnableLogs = true;
options.UseJsonSerialization = true; // To avoid the Binary Format error
options.SessionAddId = "demoApp";
options.SessionOptions.IdleTimeout = 5;
options.SessionOptions.CookieName = "AspNetCore.Session";
});
Benefits of Using NCache
- Performance Boost: NCache stores session data in memory, delivering lightning-fast response times.
- High Availability through Replication: NCache ensures session data remains accessible through data replication, providing fault tolerance and resilience.
- Linear Scalability: NCache supports adding more servers to the cluster without affecting application performance, ensuring smooth scaling as demand increases.
- Fast Dynamic Compact Serialization: Regular .NET serialization is often slow and bulky for larger objects. To handle this bottleneck, NCache has implemented a Dynamic Compact Serialization that is much faster. You just have to register your classes and NCache does the rest. No programming is needed. Upon connection time, NCache generates serialization source code, compiles it in memory, and then uses it for serialization.
Support for Multiple Regions / Zones
- WAN Replication of ASP.NET Core Sessions: NCache lets you replicate all the ASP.NET Core sessions across the WAN to another region (datacenter). This is done through a WAN Replication feature of NCache. This ensures that your ASP.NET Core sessions are never lost even if a region or zone goes down.
- Multi-Site ASP.NET Session State: If you don’t want to replicate all of ASP.NET Sessions across the WAN because of bandwidth consumption cost, you can choose to use this Multi-site ASP.NET Core Session feature of NCache. In this, the ASP.NET Session State is not replicated across sites and instead is kept at the location of its creation. But, if you move any traffic from one region or zone to another, the ASP.NET Core Session moves with it.
This allows you to have two or more active regions and zones, keep most of the traffic to its own region/zone but occasionally overflow to the other region/zone if you want. You can also bring down one region/zone without causing any interruptions for the users because their sessions will be accessible by other regions/zones.
Advanced Session Features
ASP.NET Core Session Provider for NCache implements all the standard ASP.NET Core Session Provider features. Additionally, it implements various advanced features to let you handle complex situations in your application.
- Error logging: You can enable error logging to a log file on your web server (in INSTALL_DIR\NCache\log-files\SessionStoreProvider folder) by specifying the "EnableLogs" and "WriteExceptionsToEventLog" configuration properties as follows:
services.AddNCacheSession(configuration =>
{
configuration.EnableLogs = true;
configuration.WriteExceptionsToEventLog = false;
...
});
- Standard Session Locking: The standard ASP.NET Core session locking behavior is that the session is never locked. As a result, you may corrupt the session if you try to update it simultaneously. So, NCache has implemented a session locking feature similar to the older ASP.NET Session State where if a session is locked, another request for it waits for 90 seconds (configurable) and at the end force-unlocks the session. You can specify this locking option as following properties in the "configuration":
services.AddNCacheSession(configuration =>
{
configuration.RequestTimeout = 90;
configuration.EnableSessionLocking = true;
configuration.SessionLockingRetry = -1;
configuration.EmptySessionWhenLocked = false;
...
});
- Enhanced Session Locking: For a high-traffic ASP.NET Core application, you may have robots scraping data and using the same session ID for hundreds or thousands of requests simultaneously. In this case, you cannot afford the standard session locking option because waiting for 90 seconds could tie up all your available sockets. Instead, you want to return the request quickly to indicate a failure. You can specify this as follows:
services.AddNCacheSession(configuration =>
{
configuration.EnableSessionLocking = true;
configuration.SessionLockingRetry = 5;
configuration.EmptySessionWhenLocked = true;
...
});
This makes 5 retries at half-second intervals and then returns an empty session to signify a failure. Even throwing an exception here is costly. That is why an empty session is implemented. This behavior was originally implemented on a request from a high-traffic airline website.
What to Do Next