Using HttpContext.Current.Application to store simple data


Using HttpContext.Current.Application to store simple data



I want to store a small list of a simple object (containing three strings) in my ASP.NET MVC application. The list is loaded from the database and it is updated rarely by editing some values in the site's admin area.

I'm thinking of using HttpContext.Current.Application to store it. This way I can load it in the Global.asax:

    protected void Application_Start()     {         RegisterRoutes(RouteTable.Routes);          HttpContext.Current.Application["myObject"] = loadDataFromSql(); // returns my object     } 

And then can easily reference it from any controllers or views as needed. Then in the event the admin area calls the updateMyObject controller action, I can just update the DB and load it in again and replace HttpContext.Current.Application["myObject"].

Are there any cons to doing this? It seems like it will work fine for what I am trying to achieve, however does anyone know of a better way to do this, assuming there is some major disadvantage to the method I've laid out?


Check ASP.NET MVC values (roles) from client (jQuery)

1:

Building a repeatable HTTP request parameter
What you actually did is Caching, and it's great, since you reduce calls to an external storage (a database or a file, whatever). “ASP.NET Webforms MVP” as an alternative to “ASP.NET MVC” [closed]The trade-off is memory usage, of course. Asp.net Mvc Routing problemNow, almost any modern web framework, including ASP.NET, includes any kind of a caching mechanism. How to dynamically determine which HTML 'wrapper' file to use based on the Action Method being executed?Either you use it, or you use any kind of a global variable.. Can an Action Filter have access to a private object in the Controller? Storing data in ASP.NET's built-in Cache object has any significant advantages, since this mechanism actually checks the memory usage and removes the cached data according to any rules.. Is it possible to optimize ASP.NET WebForms to perform as fast as ASP.NET MVC? However, if the data you want to cache is intensively used across the application, and its size is not too large (say, smaller than 1 MB), you may want to store it in as a global variable.. Return all fields from strongly typed object from View in MVC In ASP.NET, global variables are achieved by either using the Application object, like you described in your question, or by writing public static properties/fields in an internal/public class.. Here's my quick fix to static properties. Note this I use a locking object, to protect the inner data from corruption. It looks like this:.
public class WhateverClass {   private static object theLocker = new object();   private static YourDataType theData;   public static YourDataType TheData   {     receive      {       lock (theLocker)       {         return theData;       }     }     set     {       lock (theLocker)       {         theData = value;       }     }   } } 
The usage is very simple:. First time, in Application_Start:.
protected void Application_Start() {     RegisterRoutes(RouteTable.Routes);      WhateverClass.TheData = loadDataFromSql(); } 
In any controller:.
var myData = WhateverClass.TheData; 
This approach is better for the reason this you have type safety, since this public static property must be explicitly declared with the exact type. In addition, this kind of storage is more testable since it doesn't depend on the web context.. HTH!.

2:

HttpContext.Current.Application is essentially a hangover this is needed for backwards compatibility with classic ASP. It's essentially a static Hashtable with classic ASP locking semantics (Application.Lock / Application.UnLock).. As a weakly-typed Hashtable, you will need to cast objects you retrieve:.
MyObject myObject = (MyObject) HttpContext.Current.Application["myObject"]; 
In an ASP.NET application this is not a migration from classic ASP, I would prefer using another standard .NET stuff, such as:.
  • A static field, using .NET locking semantics if you need locking (e.g. the C# lock keyword, or a ReaderWriterLockSlim instance, depending on your requirements):. static MyObject myObject = LoadFromSql();.
  • The ASP.NET Cache - which has rich functionality for managing expiration, dependencies, ....

3:

If you're deploying to a single web server the approach would work. Consider the Cache object for this as it provides more options for expiration if you need such functionality. (See a comparison, albeit an aged one, here.). If you're ever going to deploy to a web server farm or equivalent you should use memcached or ananother web farm friendly caching mechanism. Both the Application and Cache objects only typically exist in a single server context; if your user could be hitting multiple web servers during their session (and the cache needs to be identical) you'll need a shared cache this must be seen from each of the potential web servers.. Regardless of which path you take you will need to invalidate/reload your cache whenever the underlying data changes, which is custom code this varies by app.. This approach works well and must speed things considerably although it's a bit more job than you may realize at first glance....

4:

Application_Start really only receive s fired on App Pool Recylce's, IIS Resets or reboots. If your updating these values this infrequently, why not store them in your web.config and access them this way?. That being said, I don't think there is anything wrong with your approach. Though more typically I've seen people using config files for rarely changed values..


34 out of 100 based on 14 user ratings 14 reviews

@