{"id":5332,"date":"2012-01-11T17:10:14","date_gmt":"2012-01-11T16:10:14","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/?p=5332"},"modified":"2012-01-11T17:10:14","modified_gmt":"2012-01-11T16:10:14","slug":"the-mystery-of-unexpected-expiring-sessions-in-asp-net","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/5332-the-mystery-of-unexpected-expiring-sessions-in-asp-net.html","title":{"rendered":"The mystery of unexpected expiring sessions in ASP.NET"},"content":{"rendered":"<p>This is one of those posts that will not interest you unless you have a similar problem. That said, it does illustrate one general truth, that in software problems are often not what they first appear to be, and solving them can be like one of those adventure games where you think your quest is for the magic gem, but when you find the magic gem you discover that you also need the enchanted ring, and so on. <\/p>\n<p>Recently I have been troubleshooting a session problem on an ASP.NET application running on a shared host (IIS 7.0).<\/p>\n<p>This particular application has a form with some lengthy text fields. Users complete the form and then hit save. The problem: sometimes they would take too long thinking, and when they hit save they would lose their work and be redirected to a login page. It is the kind of thing most of us have experienced once in a while on a discussion forum.<\/p>\n<p>The solution seems easy though. Just increase the session timeout.&#160; However, this had already been done, but the sessions still seemed to time out too early. Failure one.<\/p>\n<p>My next thought was to introduce a workaround, especially as this is a shared host where we cannot control exactly how the server is configured. I set up a simple AJAX script that ran in the background and called a page in the application from time to time, just to keep the session alive. I also had it write a log for each ping, in order to track the behaviour.<\/p>\n<p>By the way, if you do this, make sure that you disable caching on the page you are pinging. Just pop this at the top of the .aspx page:<\/p>\n<blockquote>\n<p>&lt;%@ OutputCache Duration=&quot;1&quot; Location=&quot;None&quot; VaryByParam=&quot;None&quot;%&gt;<\/p>\n<\/blockquote>\n<p>It turned out though that the session still died. One moment it was alive, next moment gone. Failure two.<\/p>\n<p>This pretty much proved that session timeout as such was not the issue. I suspected that the application pool was being recycled \u2013 and after checking with the ISP, who checked the event log, this turned out to be the case. Check <a href=\"http:\/\/blogs.msdn.com\/b\/johan\/archive\/2007\/05\/16\/common-reasons-why-your-application-pool-may-unexpectedly-recycle.aspx\">this post<\/a> for why this might happen, as well as the discussion <a href=\"http:\/\/blogs.msdn.com\/b\/david.wang\/archive\/2006\/01\/26\/thoughts-on-application-pool-recycling-and-application-availability.aspx\" target=\"_blank\">here<\/a>. If the application pool is recycled, then your application restarts, wiping any session values. On a shared host, it might be some else\u2019s badly-behaved application that triggers this.<\/p>\n<p>The solution then is to change the way the application stores session variables. ASP.NET has three session modes. The default is <strong>InProc<\/strong>, which is fast but not resilient, and for obvious reasons not suitable for apps which run on multiple servers. If you change this to <strong>StateServer<\/strong>, then session values are stored by the ASP.NET State Service instead. Note that this service is not running by default, but you can easily enable it, and our helpful ISP arranged this. The third option is to use <strong>SQLServer<\/strong>, which is suitable for web farms. Storing session state outside the application process means that it survives pool recycling.<\/p>\n<p>Note the small print though. Once you move away from <strong>InProc<\/strong>, session variables are serialized, not just held in memory. This means that classes must have the <strong>System.Serializable<\/strong> attribute. Note also that objects might emerge from serialization and deserialization a little different from how they went in, if they hold state that is more complex than simple properties. The constructor is not called, for example. Further, some properties cannot sensibly be serialized. See <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms973893.aspx\" target=\"_blank\">this article<\/a> for more information, and why you might need to do custom serialization for some classes.<\/p>\n<p>After tweaking the application to work with the State Service though, the outcome was depressing. The session still died. Failure three.<\/p>\n<p>Why might a session die when the pool recycles, even if you are not using <strong>InProc<\/strong> mode? The answer seems to be that the new pool generates a <a href=\"http:\/\/web2.0goodies.com\/blog\/asp-net\/asp-stateserver-you-must-set-the-machinekey\/\" target=\"_blank\">new machine key<\/a> by default. The machine key is used to encrypt and decrypt the session values, so if the key changes, your existing session values are invalid.<\/p>\n<p>The solution was to specify the machine key in web.config. See <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff649308.aspx\" target=\"_blank\">here<\/a> for how to configure the machine key.<\/p>\n<p>Everything worked. Success at last.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is one of those posts that will not interest you unless you have a similar problem. That said, it does illustrate one general truth, that in software problems are often not what they first appear to be, and solving them can be like one of those adventure games where you think your quest is &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/5332-the-mystery-of-unexpected-expiring-sessions-in-asp-net.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The mystery of unexpected expiring sessions in ASP.NET<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,67,80,96],"tags":[170,849,977],"class_list":["post-5332","post","type-post","status-publish","format-standard","hentry","category-net","category-professional","category-software-development","category-web-authoring","tag-asp-net","tag-software-development","tag-web-development"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/5332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=5332"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/5332\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=5332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=5332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=5332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}