ASP.NET Server Side State Management – Application State

June 23, 2008

no comments

ASP.NET Server Side State Management – Application State

In this post I’ll introduce the application state technique as the first
Application State ASP.NET server side state management techniques.
You can read my previous posts in the state management subject in
the following links:

What is application state?
Application state
is a global storage mechanism that saves data
that can be accessed from every web page in a web application.
The data that is saved in the Application is maintained between 
every request that is sent to the server. The application state is
maintained in the HttpApplicationState class which is implemented
as a dictionary. The Application is a good place to hold data which
is global for the web application and isn’t connected to a specific user.
By placing the data in the application state you gain a single access point
for shared application data. The data that you save in the application state
isn’t permanent and is cleaned whenever the application restarts.

Application Events
There are three main events in the application state life cycle which
can be customized. The events are:

  • Application_Start
  • Application_End
  • Application_Error

The events occur as indicated by their names – at the application start, at the
application end and when an unhandled error is raised in the application.
The events can be implement in the Global.asax file:

   public class Global : System.Web.HttpApplication

   {

 

        protected void Application_Start(object sender, EventArgs e)

        {

            // Put here global initialization code for the web

            // application

        }

 

        protected void Application_End(object sender, EventArgs e)

        {

            // Put here global clean up code for the web application

        }

 

        protected void Application_Error(object sender, EventArgs e)

        {

            // Handle the error that was unhandled in the application

            // This is the last point in web applications to handle

            // errors

        }

   }

How to Use Application State Data? 
As written earlier the application state storage is an
instance of the HttpApplicationState class. The HttpApplicationState
class is implemented as a dictionary and therefore the use of it is simple.
The only problem with the Application is that it can be updated from
multiple web pages. Therefore, you have to lock the Application object
before you make any update to data that it holds.
An example of how to update an Application variable:

   Application.Lock();

   Application["Variable"] =

      (string.Format("{0} is a string",

                     (string) Application["Variable"]));

   Application.UnLock();

In the example you can see that I concat a string to a stored variable.
Before doing the operation I use the Lock method of the Application
object to lock the object. After the operation I use the UnLock method
of the Application object. You don’t have to lock the Application when
initializing variables in the Application_Start event.
Remember to use locking whenever you update a record or else if the record
can be updated from another page the web application can become inconsistent.
The example above showed how to read a value (which need to be cast)
and how to write a value to the Application storage.

Summary
To sum up the post, the application state enable us to store global
data for our application. Even though, I prefer to use that storage with
caution because if it is over used it can have a major impact on the
memory usage of the application. The next stop in this series will be the
session state technique. 

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*