Application State

Posted on Updated on

5469596Application State

To fetch small data from a database is a slow process and takes extra time. We can overcome it by storing small amount of data on the server using Application state which is faster than accessing the database.

When the data is stored on the Application state it is available to all the users(Sessions) accessing the application. So application state is very useful to store small data that can be used across the user and will be same for all the users, we can say it as global variables that can be accessed by all the users. It can be used to store just small amount of data as the data is store on server and if we store huge amount of data on the server, it will cause performance overhead. The data in the Application State is shared amongst users via HTTP Application State class and the data can be stored using key value pair. It can also be accessed by Application property.

How Application State Works

To work with Application state, an instance of HttpApplicationState is created when the user sends the request for any resource for the first time from the application. And this can be accessed through the Application property of HTTPContext Object. This property can be accesses by all the HTTPModules and Handlers. The lifetime of these values is till the lifetime of the ASP.NET application until the application is unloaded. BAsically, the values of these Application variables are set in Application_OnStart event in Global.asax file and access and modify through ASP.NET pages.

How to Save values in Application State

The values in application state are stored as of Object type, so at the time of reading, the values it should be converted into appropriate type.

So normally, we use to store the Application wise data in Application state which is shared across the users.

We can save the data in Application_OnStart method in Global.asax file as:

void Application_Start(object sender, EventArgs e)

{

Application[“Message”] = “Welcome to Application State”;

}

We can also save object of some Class in Application variable. Let’s say we have a class as:

// Summary description for Employee

public class Employee

{

private string _name;

public string Name {

get

{

return _name;

}

set

{

_name = value;

}

}

private decimal _annualSalary;

public decimal AnnualSalary

{

get

{

return _annualSalary;

}

set

{

_annualSalary = value;

}

}

}

Put this class file in App_Code folder. Now class will be available throughout the application so also in Global.asax.

Now to save it in Application_OnStart as:

void Application_Start(object sender, EventArgs e)

{

Employee objEmployee = new Employee();

objEmployee.Name = “Brij”;

objEmployee.AnnualSalary = 1000000;

Application[“EmployeeObject”] = objEmployee;

}

Here there is no need to serialize the object to store in Application state. We can also modify these values from any method in the application.

In the below code Application[“Message”] is modified using onclick method of a button.

protected void Button1_Click(object sender, EventArgs e)

{

Application[“Message”] = “Welcome User!!”;

}

Now we’ll get the modified values whenever we try to access it.

Let us add a new variable on another button Click event as:

protected void Button3_Click(object sender, EventArgs e)

{

Application[“NewValue”] = “Hello”;

}

This value will also be available throughout the application life. The Application state is not thread safe so it can be accessed by multiple threads at the same time. Suppose a counter value is stored in the Application state and we increase it whenever a particular page is accessed. So at a single point of time, two different instances of page of different sessions can read the same value and update it. So it is quite possible that we get some unexpected result. It is required to apply some synchronisation mechanism so that at a single point of time only, one can update its value. To do so we can call the System.Web.HttpApplicationState.Lock method to set the application state value, and then to unlock it call System.Web.HttpApplicationState.UnLock method, freeing it for other write or update.

if (Application[“Counter”] != null)

{

Application.Lock();

Application[“Counter”] = ((int)Application[“Counter”]) + 1;

Application.UnLock();

}

So by this way, we can avoid writing the same value from multiple Threads at the same time.

How To Read Values from Application State

It is very easy to read the values from the Application state. What we have to do is to just check whether the value we are accessing is null, and if so, it will return null and at the time of casting it in different datatype it will throw an exception. As the data in Application state is stored as object to it is required to typecast it.

if(Application[“Message”] !=null)

{

string message = Application[“Message”] as string;

}

The object also can be read as:

if (Application[“EmployeeObject”] != null)

{

Employee myObj = Application[“EmployeeObject”] as Employee;

string Name = myObj.Name;

}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s