I use the following code to count the number of currently open sessions in my ASP.NET (2.0/3.5) application (ASMX web service, legacy code), but if it runs long enough the count stops matching the built in performance counters (my count is higher, Session_End seems to not be invoked sometimes). The sessions are InProc. What might I be missing?
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Application["OpenSessionCount"] = 0;
}
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["OpenSessionCount"] = (int)Application["OpenSessionCount"] + 1;
Application.UnLock();
/* Set other Session["foo"] = bar data */
}
protected void Session_End(Object sender, EventArgs e)
{
Application.Lock();
Application["OpenSessionCount"] = (int)Application["OpenSessionCount"] - 1;
Application.UnLock();
}
}
"Just use performance counters!"
Yes, I'm just asking because I'm curious where I went wrong.