Tech Off Thread

11 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

ASP.NET Session Timeout Ignored

Back to Forum: Tech Off
  • User profile image
    mrichman

    I am trying to alter the session timeout for a simple ASP.NET app.

    In my web.config I set this:

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

    I set the start time in session_start:

    void Session_Start(object sender, EventArgs e)

    {

    Session["StartTime"] = DateTime.Now;

    }



    Then, I refresh the page every second, displaying the session id and elapsed time for the session:

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)

    {

    DateTime startTime = (DateTime)Session["StartTime"];

    TimeSpan ts = DateTime.Now.Subtract(startTime);

    lblTimeSpan.Text = ts.Seconds.ToString();

    }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head id="Head1" runat="server">

    <title>Untitled Page</title>

    <meta http-equiv="refresh" content="1">

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    Session ID <%= Session.SessionID %> has been active <asp:Label ID="lblTimeSpan" runat="server"></asp:Label> seconds.<br />

    Session.Timeout = <%= Session.Timeout %>

    </div>

    </form>

    </body>

    </html>


    Every time, without fail, no matter what I do to try to manipulate Session.Timeout, the session expires after 60 seconds!

  • User profile image
    Maurits

    Are your refreshes passing the session cookie?

  • User profile image
    mrichman

    Maurits wrote:
    Are your refreshes passing the session cookie?


    I assume so, considering the session variable holding "StartTime" is stateful between successive page loads, and the SessionID is constant until the session expires. Also, as a test, I tried setting cookieless="true" which also produces the same results Mad

    Thanks,

    Mark

  • User profile image
    Maurits

    Is it possible the process is crashing and being restarted every 60 seconds?  Try hooking the Application start event as well.

  • User profile image
    mrichman

    Maurits wrote:
    Is it possible the process is crashing and being restarted every 60 seconds?  Try hooking the Application start event as well.


    The Application is indeed being reset after 60 seconds! Interestingly, the Session ID remains constant even across server-side expiration, presumably because it's stored in the cookie.

  • User profile image
    Rossj

    mrichman wrote:
    
    Maurits wrote:Is it possible the process is crashing and being restarted every 60 seconds?  Try hooking the Application start event as well.


    The Application is indeed being reset after 60 seconds! Interestingly, the Session ID remains constant even across server-side expiration, presumably because it's stored in the cookie.


    I used to get this (app restarts) when either-

    1. AntiVirus was touching a file and forcing a re-compile..
    2. I ran out of memory and the runtime restarted

  • User profile image
    mrichman

    This is happening on 4 of my production servers, as well as my desktop machine. None have antivirus, and each has 4GB of RAM. Since I can reproduce it on my desktop using the built-in webserver in Visual Studio, I can rule out a w3wp worker process recycle as the culprit.

  • User profile image
    Maurits

    Anything in the event logs, or the website logs?

  • User profile image
    W3bbo

    Maurits wrote:
    Is it possible the process is crashing and being restarted every 60 seconds?  Try hooking the Application start event as well.


    I doubt that.

    I had a problem where Session_End was never fired, I wonder if its related.

  • User profile image
    mrichman

    Everything in ASP.NET works fine, as expected.

     

    I had a minor error in my code which led me to believe the application was restarting. Replace .Seconds with .TotalSeconds to see it work:

     

    protected void Page_Load(object sender, EventArgs e)

        {

            DateTime startTime = (DateTime)Session["StartTime"];

            TimeSpan ts = DateTime.Now.Subtract(startTime);

            lblTimeSpan.Text = ts.TotalSeconds.ToString();

     

            if (ts.TotalSeconds > 10)

            {

                Session.Abandon();

            }

           

            DateTime appStartTime = (DateTime)Application["AppStartTime"];

            TimeSpan ts2 = DateTime.Now.Subtract(appStartTime);

            lblAppTimeSpan.Text = ts2.TotalSeconds.ToString();

        }

     

    In this example, the session will expire after 10 seconds, but the application keeps going.

  • User profile image
    Maurits

    Ah... well, if you're explicitly CALLING Session.Abandon()...

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.