Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Working with the Application Object

Not all data in your Web applications is specific to individual users of your Web site. For instance, if your application tracks stock prices, this data should be available to all users. Data like this is called global data, and using global data in session doesn't make sense. Fortunately, ASP.NET provides the Application object just for this purpose.

The Application object is stored in memory and unlike Session, objects can't be offloaded to another server or a SQL database. This limits the usefulness of the Application object for Web farm scenarios. However, if your Web application contains a large amount of read-only data that takes awhile to load, the Application object is a good place to store it.

Adding Objects to the Application

You can add objects to the Application object in the same way that you can in Session objects. For instance,

Application("WebStoreName") = "Stanley Sprockets Online"

stores a string into the Application object. Conversely,

Dim strStoreName As String = Application("WebStoreName")

fetches the value.

Synchronizing Code in the Application Object

If more than one Web page tries to store or update a value in the Application object at the same time, the data can become corrupt unless the updates are synchronized. This situation could easily happen with a site that experiences even a moderate amount of traffic, so using the Lock() and Unlock() methods of the Application object is important when you're storing data. Listing 3.7 shows how to synchronize code properly.

Listing 3.7  HitCounter.aspx: Updating a Hit Counter for the Web Site

<%@ language="VB" %>
<%
   `We need to check if the "Hits" object 
   `has never been assigned and is null.
   `If it is null, then we set it equal to 0
   if Application("Hits") Is Nothing Then 
     Application.Lock()
     Application("Hits") = 0
     Application.UnLock()
   End If
   Dim nHits As Integer = Application("Hits")
   nHits = nHits + 1
   Application.Lock()
   Application("Hits") = nHits
   Application.UnLock()
%>
<html>
<body>
<h2>This site has been hit 
    <% Response.Write(Application("Hits").ToString()) %> 
    times </h2>
</body>
</html>

CAUTION

Locking the Application object blocks other pages that use the object from running until it is unlocked. For best performance, don't make many updates to the Application object. The best use for the object is for infrequently updated read-only data. An example might be an application that tracks sporting events and stores the latest scores from current games every five minutes.

Removing Objects from the Application

You can remove objects from the Application object by using the Remove() and RemoveAll() methods, just like with the Session object.

Lifetime of an Application

The Application object is created on the first hit from the first user to your ASP.NET application. The Application object remains in memory until the Web server is shut down, or the web.config site configuration file has changed. (We introduce the web.config file shortly.) Unlike the Session object, the Application object doesn't have an Abandon() method.

  • + Share This
  • 🔖 Save To Your Account