Get total number of users accessing web application

In my application , we have to display the total number of users who were accessign the hosted web application at any point of time . To implement this requirement , i have made some changes in the Global.Asax file of the application . Here is the code snippet.Hope this will help  you .

<%@ Application %>

<script RunAt=”server”>

void Application_Start(object sender, EventArgs e)

{

// Code that runs on application startup

Application.Add(“ActiveUsers”, 0);

}

void Application_End(object sender, EventArgs e)

{

// Code that runs on application shutdown

}

void Application_Error(object sender, EventArgs e)

{

// Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e)

{

// Code that runs when a new session is started

Application.Lock();

Application[“ActiveUsers”] = System.Convert.ToInt32(Application[“ActiveUsers”]) + 1;

Application.UnLock();

}

void Session_End(object sender, EventArgs e)

{

// Code that runs when a session ends.

// Note: The Session_End event is raised only when the sessionstate mode

// is set to InProc in the Web.config file. If session mode is set to StateServer

// or SQLServer, the event is not raised.

Application.Lock();

Application[“ActiveUsers”] = System.Convert.ToInt32(Application[“ActiveUsers”]) – 1;

Application.UnLock();

}

</script>

 Now you can retrieve number of active users     —  Application.Get(“ActiveUsers”).ToString()

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s