Code to handle application events (such as the start and end of an application) resides in Global.asax. Such event code cannot reside in the ASP.NET page or web service code itself, since during the start or end of the application, its code has not yet been loaded (or unloaded). Global.asax is also used to declare data that is available across different application requests or across different browser sessions. This process is known as application and session state management.
The application object is created when the first request by the user is made and is global across all users and all web visitors. It is removed from memory after inactivity or shut down by web server.
A session object is created when the first page request is made by each user. It is global across all web pages and unique for each user. It is removed from memory after a specified length of time when the user is inactive.
To illustrate, I will demonstrate a simple example that will track current number of users that are working with a web site.
1. Create an empty ASP.NET web site in Visual Studio.
2. Click on “Website” at the top of the menu and select “Add New Item”.
3. Choose “Global Application Class” from the dialog box and click “OK”.
Click to enlarge
4. The application object and session objects both have an internally managed collection to enable you to add some values. To start off let’s add a “userCount” variable and initialize it to zero in the “Application_Start” event handler.
example code:
void Application_Start(object sender, EventArgs e)5. The next step is to increment and set the “userCount” whenever a new visitor comes to the web site. This is done in the “Session_Start” event handler.
{
Application.Add("userCount", 0);
}
example code:
void Session_Start(object sender, EventArgs e)6. Once the user leave the web site the “userCount” field must be de-incremented.
{
int userCount = int.Parse(Application.Get("userCount").ToString());
userCount++;
Application.Set("userCount", userCount);
}
This is done in the “Session_End” event handler.
example code:
void Session_End(object sender, EventArgs e)7. The only thing left to do is to display the “userCount” on the web page. Create a “default.aspx” page in your project and enter the following in the “Page_Load” method of the “default.aspx.cs”.
{
int userCount = int.Parse(Application.Get("userCount").ToString());
userCount--;
Application.Set("userCount", userCount);
}
example code:
protected void Page_Load(object sender, EventArgs e)8. Now you are ready to test your changes. Start a dubugging session by pressing F5. Your default browser should come up and the text “UserCount: 1” should show at the top of the page. To increment the “userCount”, copy the web address at the top of the browser and open a second browser on your machine. The “userCount” should now be 2. If you wait a while (typically 20 minutes), the session will expire and the “userCount” will de-increment after the browser is refreshed.
{
Page.Response.Write("UserCount: " + Application.Get("userCount").ToString());
}
Download the complete code to this project >>here<<
No comments:
Post a Comment