I plane to use static variables instead of Application state in ASP.NET and am wondering if this is correct approach:
[Global.asax.cs]
...
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
...
private static Dictionary<string, object> cacheItems = new Dictionary<string, object>();
private static object locker = new object();
public static Dictionary<string, object> CacheItems
{
get
{
lock (locker)
{
return cacheItems;
}
}
set
{
lock (locker)
{
cacheItems = value;
}
}
}
public static void RemoveCacheItem(string key)
{
cacheItems.Remove(key);
}
...
}
As you can see I use automatically created Global.asax (and code behind) file. I’ve added some static variables and methods. I can use them after in this manner:
[some .cs file]
foreach(KeyValuePair<string, object> dictItem in Global.CacheItems)
{
...
Is this the right way or I should create new class instead of existing Global? If I should create new class how can I do that and where?
What Microsoft says
reference : http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607
My experience
The main different between static variables and Application state, is that the Application state is the same across all threads and pools, but the static is the same only per pool.After new tests I see that the Application state variables are the same as static variables, and they just reference to a static variable on the application, and they just exist for compatibility reasons as Microsoft says
If you have 4 pools running your site (web garden) then you have 4 sets of different static memory.
Your code
About your code, you have bug for the way you try to access your dictionary data, and you will going to have errors in real web. This part of the code is lock the variable of the full Dictionary but not lock the change you going to make when you use it.
The correct approach is to lock all actions of add/remove until you done, for example.
From the other hand when you manipulate data on application state you need to use the
globallock of itApplication.Lock();andApplication.UnLock();for exampleTo close with some conclusion:
Avoid Application state and just use static variables on your code.