locked
Why is Session_End not firing? RRS feed

  • Question

  • User1227509684 posted

    Web.Config:

    ===============================================================================

      <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <httpModules>
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
    <customErrors mode="Off"/>
    <trust level="Full"/>
    <sessionState mode="InProc" timeout="1" />
    </system.web>

    ===============================================================================

    Global.asax:

    ===============================================================================

    using Movies.Models;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;

    namespace Movies
    {
    public class MvcApplication : System.Web.HttpApplication
    {
    protected void Application_Start()
    {
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    Database.SetInitializer<MovieDBContext>(null);
    }

    protected void Session_End(object sender, EventArgs e)
    {
    ;
    }

    }
    }

    My breakpoint in Session_End is never hit

    Sunday, December 16, 2018 9:41 PM

All replies

  • User475983607 posted

    There is no indication in the posted code snippet that Session is set.  Session has a default 20 minute timeout.  Are you waiting for Session to timeout?

    Please see the Session reference documentation and make sure you know how Session works.

    https://support.microsoft.com/en-us/help/300883/how-to-use-session-and-application-variables-in-an-asp-program

    Sunday, December 16, 2018 10:58 PM
  • User1520731567 posted

    Hi HarpoMarx,

    Please refer to this link:

    https://forums.asp.net/t/1384474.aspx?Session+State+TimeOut+and+Session_End+event

    1.While thinking of Session_End event one of the most important things is that the event is supported only when the session is maintained Inproc. The session_End event will not fire if you are using State server or SQL server.

    2. Another important point to note is that Session_End event is fired by a worker process. This also means that Session_End event might not have all the permission that a normal page request has. So if you are trying to connect to the SQL server with Windows account, Session_End event might not have required permission, although you can connect to the SQL in Session_Start event.

    3. Session_End will fire after a given amount of time where session has been inactive. The given time can be changed globally in the web.config file with the time out attribute or for individual session with the help of Session.Timeout property.

    4. Session_End will also fire when someone calls the Session.Abondon method. Note that the Session_End event will not fire immediately. As said before the Session_End event is fired from worker process. Hence the session ID will also be same.

    5. Remember the Session_End event will not fire in case the user closes the browser. HTTP is a stateless protocol. There is no way for the server to understand that the browser has been closed.

    6. Also another very important thing to note here is that if you do not save anything in the session the Session_End event will not fire. There must be something saved in the session atleast once for the Session_End event to fire. This also means that if you save something in the session in the first request and abandon the session in the same request the Sesison_End event will not fire, as there was nothing saved in the session ever.

    I saw you have set:

    <sessionState mode="InProc" timeout="1" />

    in web.config.

    Is your Session_End empty?

    If the code in the Session_End method is wrong, it will return immediately, and no error will be logged, no exception will be thrown, and the error can not be output by using the Respone.Write method.

    Server_MapPath, Request, Cache, HttpContext.Current, etc. cannot be used in the Session_End method.

    In addition,please refer to this link:

    https://stackoverflow.com/questions/4813462/session-end-does-not-fire

    Also another very important thing to note here is that if you do not save anything in the session the Session_End event will not fire.

    There must be something saved in the session atleast once for the Session_End event to fire.

    This also means that if you save something in the session in the first request and abandon the session in the same request the Sesison_End event will not fire, as there was nothing saved in the session ever.

    Maybe there is no any Session data in your code,Without this, Session_End will not be fired.

    Finally,How to implement it, you could refer to this link and try to make a demo:

    https://forums.asp.net/t/1641609.aspx?Session_End+equivalent+in+MVC

    Hope my answer will helpful to you.

    Best Regards.

    Yuki Tao

    Monday, December 17, 2018 6:29 AM
  • User1227509684 posted

    Thanks, Yuki!  Fixed the problem.  Changed timeout value from 1 to 2 (said use minimum value of 2) but more importantly -- I had to put this in:


    protected void Session_Start(object sender, EventArgs e)
    {
         Session["Movies"] = "x";
    }

    Thursday, December 20, 2018 2:41 AM
  • User753101303 posted

    Hi,

    So more likely it is just because you are not using it. Similarly if you check the session id without storing anything it will change with each and every request. In short sessions are handled if you actually stored something in session. Else this step is skipped.

    Note also that some providers are not firing this event. My personal preference is to possibly use Session_End as an enhancement but to design the app so that it doesn't really matter if for some reason this event doesn't happen.

    Thursday, December 20, 2018 4:24 PM