Introduction
ASP.NET offers you a variety of ways to maintain state information on the server rather than persisting information on the client. In these set of articles I will explain ASP.NET server-based state management options: Application state, session state, profile properties, and database support.
Server-based state management options
Server-side options for storing page information typically have higher security than client-side option. But they can use more web server resources, which can lead to scalability issues when the size of the information store is large.
1. Application state:
Is a method of storing global application specific information that in visible to the entire application. Data is shared by multiple sessions and accessible from all pages in the web application.
2. Session state:
This is available as a method of storing session-specific information that is visible only within the session. Values are persisted for the duration of that session only.
3. Profile properties:
This feature allows you to store user-specific data. It is similar to session state, except that unlike session state, the profile data is not lost when a user's an ASP.NET profile, which is stored in persistent format and associated with an individual user.
4. Database support:
In same cases, you might want to use database support to maintain state on your website. Typically database support is used in conjunction with cookies or session state.
Application state
Application state is stored in an instance of the HTTPApplicationState class. Which exposes a key-value dictionary of objects, the data is available to all classes in an ASP.NET application. Application state is stored in memory on the server and it is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users' and sessions.
The HTTPApplicationState instance is create the first time a user accesses any URL resource in an application and is often accessed through the application property of the HTTPContext class.
You can use application state in two ways:
- Add, access, or remove values from the contents collection directly through code, it is often useful to load application state data when the application starts to do so, put your code into the Application-Start method in the Global.asax file.
- You can add objects to the StaticObjects collection via an <object runat=â€Serverâ€> declaration in your web application’s Gloabal.asax.You can add objects to the “staticobects†collection only in the Global.asax file.If you attempt to directly add objects through code the collection will throws a NotSupportException.
ASP.NET |
<object RunAt="server" Scope="application" Id="MyData" ProgId="MSWC.MyData" />
|
You can access members of objects defined in the StaticObjects collection without having to reference the application collection.
C# |
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = MyData.Title;
}
|
Application state consideration Resources:
Because it is stored in memory, application state is very fast compared to saving data to disk or a database.
Volatility: Because it is stored in memory, it is lost whenever the application stopped or restarted.
Scalability:
Application state is not shared among multiple servers serving the same application as in web form, your application therefore cannot rely on application state containing the same data for application state across different servers or processes.
Concurrency: Application state is free-threaded, which means that application state data can be accessed simultaneously by many threads. Therefore, it is important to ensure that when you update state data, you do so in thread-safe manner.You can use lock and unlock methods to ensure data integrity by locking the data for writing by only one source at a time.
How To: Save values in application stateApplication state stores data as objects, to save a value to the HTTPApplcationState class you can use the application property.
C# |
Application["WelcomeMessage"] = "Welcome to Innovation-Hut";
|
How To: Read values from application state
First
you have to determine whether the application variables exist, and then convert the variable to the appropriate type when you access it.
C# |
if (Application["NumberOfVisitoes"] != null)
{
int total = (int)Application["NumberOfVisitoes"];
}
|
How To: Wrtie values to application state with lockingTo lock the writing in the application state to only one source you can call lock method in the HTTPApplicationState class then set the application state value and finally call the unlock method.
C# |
Application.Lock();
Application["info"] = "Technology is life";
Application.UnLock();
|