ASP.NET 1.1 Performance Guidelines - Application State

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, John Allen, and Alex Mackman


Use Static Properties Instead of the Application Object to Store Application State

You should store data in static members of the application class instead of in the Application object. This increases performance because you can access a static variable faster than you can access an item in the Application dictionary. The following is a simplified example.

<%
private static string[] _states[];
private static object _lock = new object();
public static string[] States
{
  get {return _states;}
}
public static void PopulateStates()
{
  //ensure this is thread safe
  if(_states == null)
  {
    lock(_lock)
    {
       //populate the states…    }
  }
}
public void Application_OnStart(object sender, EventArgs e)
{
  PopulateStates();
}
%>



Use Application State to Share Static, Read-Only Data

Application state is application-wide and specific to a server. Even though you can store read-write data, it advisable to only store read-only data to avoid server affinity. Consider using the Cache object. The Cache object is a better alternative for read-only data.


Do Not Store STA COM Objects in Application State

Storing STA COM objects in application state bottlenecks your application because the application uses a single thread of execution when it accesses the component. Avoid storing STA COM objects in application state.

Personal tools