SIGN UP MEMBER LOGIN:    
ARTICLE

Hour 5: Understanding 5 ASP.NET State Management Techniques in 5 hours

Posted by Vishal Nayan Articles | ASP.NET Programming April 28, 2011
Application State stores global objects that can be accessed by any client.
Reader Level:
Download Files:
 


Application State: stores global objects that can be accessed by any client.

It supports the same types of session state support that are of type objects, retains information on the server, and uses the same dictionary-based syntax. Application state is based on the System.Web.HttpApplicationState class, which is provided in all web pages through the built-in Application object.

Points to remember:

  1. application state items are stored as objects, so you need to cast them when you retrieve them from the collection.
  2. Items in application state never time out.
  3. Application state information is always stored in a process. This means you can use any .NET data types.
     
  4. Application state isn't often used, because it's generally inefficient and its rarely used in practical scenario because its most common uses have been replaced by easier and more efficient methods.
     
  5. We use application state to store database connection strings, which are stored now in a web.config file, which is more flexible.
  6. Previously application state was used to store more frequent data like full product; now you can use an ASP.NET cache, which are more efficient.

Example: We are often interested in knowing how many times a given page has been requested by various clients. Using an application state variable we can do this:

protected void Page_Load(object sender, EventArgs e)
    {
        int count = 0;
        if (Application["HitCounterForOrderPage"] != null)
        count = (int)Application["HitCounterForOrderPage"];
        count++;
        Application["HitCounterForOrderPage"] = count;   
        lblcounter.Text = "Page Visited: " + count.ToString() + "Times";       
    }


Problem with this approach: In the above code, a page counter would probably not keep an accurate count, particularly in times of heavy traffic. For example, if two clients requested the page at the same time, you could have a sequence of events like this
  1. User A retrieves the current count (432).
  2. User B retrieves the current count (432).
  3. User A sets the current count to 433.
  4. User B sets the current count to 433.

To avoid this problem you can use a mechanism to lock and unlock application state:

Application.Lock();
        int count = 0;
        if (Application["HitCounterForOrderPage"] != null)
        count = (int)Application["HitCounterForOrderPage"];
        count++;
        Application["HitCounterForOrderPage"] = count;
Application.UnLock();
lblcounter.Text = "Page Visited: " + count.ToString() + "Times";  

When to use: You are storing infrequently changed, application-scope information that is used by many users, and security is not an issue. Do not store large quantities of information in an application state object.

This is end of your fifth hour of reading. Hope you enjoyed it.

Login to add your contents and source code to this article
share this article :
post comment
 

thanks dinesh :D

Posted by Vishal Nayan May 20, 2011

Hi, very good post,realy clear, understanding and useful. thanks and keep it up.

Posted by Dinesh Bagne May 20, 2011

Thanks for feedback :D

Posted by Vishal Nayan Apr 30, 2011

it was a good read..I read all 5 ..good work

Posted by sat mami Apr 29, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor