About Me

My photo
Hello, I'm a technology geek looking to upgrade my programming skills. These are some of the things I'm learning along my journey.

Thursday, June 14, 2012

Working with the Global.asax file

The Global.asax is an optional file that contains code to handle global events in your ASP.NET web site. It has handlers for the application and session object.

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)
    {
       Application.Add("userCount", 0);
    }
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.

example code:
void Session_Start(object sender, EventArgs e)
    {
       int userCount = int.Parse(Application.Get("userCount").ToString());
       userCount++;
       Application.Set("userCount", userCount);
    }
6. Once the user leave the web site the “userCount” field must be de-incremented.
This is done in the “Session_End” event handler.


example code:
void Session_End(object sender, EventArgs e)
    {
       int userCount = int.Parse(Application.Get("userCount").ToString());
       userCount--;
       Application.Set("userCount", userCount);
    }
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”.


example code:
    protected void Page_Load(object sender, EventArgs e)
    {
       Page.Response.Write("UserCount: " + Application.Get("userCount").ToString());
    }
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.

Download the complete code to this project >>here<<.


No comments:

Post a Comment