Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I wanna free some DB resources and set few flags when User Session Ends in ASP.NET Site. But when I write my code to access session variables in Session End method of Global.asax file, its getting called everytime the App starts, Why this weird behavior?

Secondly, I want to access user specific session variables and free them up in DB on Session End. Thus I am using few session Variables where I am setting them in a webmethod on a Page and trying to access them on Session End. But since Session end is being called on App start up its always throws Null reference exception.

Here is mycode. for Setting up a variable in Webmethod in a .aspx page
C#
[WebMethod(EnableSession=true)]
protected void checkUser()
        {
            Session["TestObject"] = "Hello I am session";
}


In my global.asax file I am trying to access as follows in Session_End method

C#
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.
            if (this.Context.Session != null && this.Context != null)
            {
                string k = this.Session["TestObject"].ToString();
            }
            
        }


I even tried HttpContext.current.Session etc.. but none worked. All are throwing exception as Session end is called on App start up and even when session timed out.
Posted
Comments
Praveen Kumar Upadhyay 17-Jul-15 5:52am    
First of all your problem is weird. Session_End should only be called when a user session end up. May be we need more explanation and code.

1 solution

This may not be answer for your question, but refer below link it explains about same weird scenario when Session_End get called.

ASP.NET Session End Event Fires Immediately After Session Start - Resolved[^]

See if the same is happening with your session state.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900