Microsoft is favoring ASP.NET Core over the legacy ASP.NET and plans on making all future developments in the former. Meaning, ASP.NET is slowly becoming obsolete. Hence, developers are switching to ASP.NET Core to reap the benefits of all future updates and technologies.
But, the users who have been using ASP.NET for years can’t make this switch so smoothly. They instead switch some of their applications to ASP.NET Core rather than all of them. This configuration works for all scenarios except one, session sharing. Meaning, the users can’t use or store ASP.NET sessions in ASP.NET Core applications and vice versa.
The solution is to cache sessions in a distributed cache such as NCache and let it handle session sharing between your web applications for you.
Share Session Data Using NCache
Session data can’t be shared between ASP.NET and ASP.NET Core applications. This is because they both serialize their session data differently and use different data structures. NCache overcomes these problems by storing session data in the cache using its serialization method and an interim data structure. Hence, allowing session data to be readable across ASP.NET and ASP.NET Core applications.
The process to configure session sharing using NCache is also simple. All you have to do is download and install the NuGet packages NCache provides for ASP.NET and ASP.NET Core and make some changes in the configuration files. Just follow the steps below:
Install NuGet Packages for ASP.NET and ASP.NET Core
The first step is to install the respective NuGet packages that NCache provides for session sharing between ASP.NET and ASP.NET Core applications. NCache provides the AspNet.SessionState.NCache NuGet package for ASP.NET and the AspNetCore.Session.NCache NuGet package for ASP.NET Core.
Configure ASP.NET Application
Once you have installed the NuGet package for your ASP.NET application, you need to make some changes to the web.config file of your application. Under the
sessionState tag, you need to ensure that the
cookieName property has the same value for both ASP.NET and ASP.NET Core applications.
providers tag, ensure that
sessionAppId has the same value for both applications and the
enableSessionSharing property has the value
true. If any of these properties are missing, make sure to add them. A sample configuration has been provided below:
Configure ASP.NET Core Application
Once you have installed the NuGet package for your ASP.NET Core application, you need to make some changes to the appsettings.json file of your application. Just like for ASP.NET, you need to ensure that the
SessionAppId attributes have the same value for both applications and the
EnableSessionSharing attribute has the value true.
After this, you must use the extension methods provided by NCache for session sharing (explained later). A sample appsettings.json file configuration is provided below:
"CacheName": "demoCache", //Replace "demoCache" with the name of your cache
"EnableSessionSharing": true, // Default value is false
"CookieName": "ASP.NET_CORE_SessionId", // For session sharing the cookie name for the web applications needs to be the same.
NCache Extension Methods for Session Sharing
NCache provides extension methods for the ASP.NET Core session interface that supports adding and fetching custom objects and automatically handles the serialization/deserialization of objects for you. This takes away the need to use binary/JSON converters or write code to serialize/deserialize data at the client end.
If the user wants to enable session sharing between ASP.NET Core and ASP.NET applications, they must use these extension methods to store and retrieve their session data. NCache can only deserialize and serialize objects between the user’s ASP.NET Core and ASP.NET sessions if they use these extension methods.
The following code example adds a custom object into the ASP.NET Core session using the
Set method provided by the NCache extension method for ASP.NET Core.
string key = "ProductID:1001";
Product customObejct = GetProduct(1001);
// Add custom object to session with against key
The following code example fetches a custom object from an ASP.NET Core session using the
TryGetValue method provided by the NCache extension methods for ASP.NET Core:
string key = "ProductID:1001";
// Fetch custom object against key it was added against ("ProductID:1001")
HttpContext.Session.TryGetValue(key, out customObject);
Benefits of Session Sharing Using NCache
No other caching vendor or otherwise provides this feature of session sharing between ASP.NET and ASP.NET Core applications, only NCache provides this feature. Not only that but NCache has also made its configuration very simple and non-invasive.
The user just has to install the respective NuGet packages, make changes in the respective configuration files (web.config for ASP.NET and appsettings.json for ASP.NET Core), and use the extension methods provided by NCache to store/retrieve their sessions. Users of both these web frameworks can greatly benefit from this feature and share their session data across both frameworks seamlessly.
Like session sharing, NCache also provides many other rich, efficient, and easy to configure and use features for all of its users. Session sharing is just among the other features that are unique to NCache and don’t exist anywhere else on the market. Don’t believe me? Try NCache now and check them out for yourself!