//
you're reading...
ASP.NET

Hour 5: Understanding 5 ASP.NET State management techniques in 5 hours


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

It supports the same types what session state support which 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 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

1)    We use application state to store database connection string, which is stored now in web.config file, which is more flexible

2)    Previously application state was used to store more frequent data like full product , now you can use ASP.NET cache , which are more efficeint.

Example: We are often interested into knowing how many times a given page has been requested by various cleint. Using 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 above code, 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 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 5th hour of reading Hope you enjoyed it.

Advertisements

About Vishal

Vishal Nayan is a seasoned professional with hand on Experience on Mircrosoft Technologies. He always look for challenging IT position that allows him to learn new Microsoft Technologies while utilizing experience of Project Development and Software Engineering Ethics. A MCP in WCF ,and looking forward for more.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow Vishalnayan on WordPress.com

Blog Stats

  • 210,065 hits
%d bloggers like this: