Step
1: Create a New Project in Visual
Studio 2008 as shown in below Figure:
Step 2: Select
Asp.net Web Application template from the list of template and name it
DemoApplication as shown below:-
Step 3:- Now we need to add global.asax file. So right Click on your website name from Solution explorer ->
Add New Item -> Global Application Class. It will be added in the root
directory of your website with the name "Global.asax" as shown
in below two images.
Step 4: First
Modify Default.aspx page as below:
Step 5:- Now open
global.asax file
- From the above image we can see that Global class is inheriting from System.web.HttpApplication.
- It methods, properties, and events that are common to all application objects in an ASP.NET application
- Now in order to explore more about httpApplication class we are taking example of calculating total No. of User currently accessing a webpage.
Global.asax.cs Code:
protected void Application_Start(object sender, EventArgs e)
{
Application["UserCount"] = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
Application.Lock();//locks access to httpapplicationstate variable to facilitate access synchronization
Application["UserCount"] = Convert.ToInt32(Application["UserCount"]) + 1;
Application.UnLock();//release the locks.
}
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();//locks access to httpapplicationstate variable to facilitate access synchronization
Application["UserCount"] = Convert.ToInt32(Application["UserCount"]) - 1;
Application.UnLock();//release the locks
// Session.Abandon();
}
Global.asax code
Explanation:-
Application_Start:- Here
we can place code which runs on application start.Here we have declare
Application variable Application[“UserCount”] which is initialized to zero to
keep track of user who have currently opened a webpage.
Session_Start:- This
method includes code that runs when a new session is started.In this method
first we are locking an application object Application[“UserCount”] to
facilitate access synchronization and then we are incrementing the variable
value which implies that a new user has started a session and then releasing a
lock,so that other users session data can be captured.
Session_End:- This
method includes code that runs when a session is ended. In this method first we
are locking an application object Application[“UserCount”] to facilitate access
synchronization and then we are decrementing the variable value to indicate
user session has ended and then releasing a lock, so that other users session
data can be captured.
Step 6: Now run
the application by pressing f5
Step 7:- Now open
different browser and copy the url from the first browser and paste it in the
new open browser.
By opening a new browser. It is creating a new session for
the user on the same machine therefore it is incrementing an application object
value.
Note:-We can also
set session timeout property in the web.config file to timeout the session
after a certain interval of time.