'Is there a replacement for `HttpApplicationtState` in asp.net core
I am migrating a legacy application written in Asp.Net MVC framework to ASP.net core MVC and some classes use HttpApplicationState to share cache object between all the users of the app
I've tried to read online about a replacement for this object (like IFormFile replacing HttpPostedFile) but could not find any information regarding this object
/* This method being called from the global.asax file in framework app and
usses the HttpApplicationState */
public static void Foo(HttpApplicationState app)
{
app.Set("Cache", cache);
}
I would like to know how can i achieve a similar behavior in my migrated application
Solution 1:[1]
The answer to this question is very simple.
First, know that the HttpApplicationState object stores data for all users and sessions to use, quote "Unlike session state, which is specific to a single user session, application state applies to all users and sessions.".
Therefore, you can look at the HttpApplicationState object as a basic caching object that is shared across all.
So to answer your question, the replacement for the HttpApplicationState object is:
IMemoryCachewhich could be the literal replacement ofHttpApplicationStateif you want to replicate (more or less) the legacy applicationIDistributedCachewhich is much more recommended nowadays
Solution 2:[2]
You can add a singleton in the DI container:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ClassToStoreData>();
}
After this you can access the data by adding this parameter to the constructor of any class:
public class SomeClassThatUsesCache
{
public SomeClassThatUsesCache(ClassToStoreData cache)
{
}
}
The data inside ClassToStoreData will be available to all users and any place in your application
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ray |
| Solution 2 | Ray |
