Why .Net sessions are terminated/loss unexpectedly

There are many cases when ASP.Net website works great in development environment but as we migrate our website to production server, it starts giving unexpected session timeouts. This article discuss those problems and provide solutions to resolve them.

Here are some of the reasons why this happens:- 

Cause 1: IIS Settings:
1- Application Pool is recycled. - We will know this looking at the system logs
2- IIS/worker process is restarted. - System logs will tell this as well
3- Application Domain is restarted. - We need to monitor for application restarts for the ASP.NET counter in perform to check this.
4- IIS worker process can get recycled depending on the configuration, low on virtual memory, crash due to unhanded exception etc.

Resolution:
1- Goto Start->run->inetmgr->Application pools.
2- Select your application pool and right click -> properties.
3- And see the settings for Recycle worker process (in minutes), set an appropriate value there.
4- Alternatively you can recycle your process when your site generally stays idle i.e. you can select 'You can set values in Recycle worker process at time' and give appropriate time to recycle process.

Cause 2: Modifications in Application Contents:
1- Bin folder of the application is modified.
2- Web.config or the machine.config is modified.
3- Global.asax file is modified.
4- Something in the code is causing session loss, it can be anything like you are adding/removing files in your application folder through code e.g. uploading images. You will need to look into the code to have a fix on this. Like Session.Abandon() or Session.Clear();

Resolution:
Try stopping anti virus software on server and see that session as loosing frequently or not. If problem solves after that then Exclude the anti virus scanning from the IIS/ASP.NET default folders and your application folders.
a- <drive>:\WINDOWS\system32\inetsrv
b- <drive>:\WINDOWS\assembly\GAC_32
c- <drive>:\WINDOWS\Microsoft.NET\Framework\
d- Any application directory containing web.config files, global.asa or global.asax, .net assemblies, and/or other web content which your web apps use.

Check your code, that if your application looses session after a particular operation i.e. you might be changing the contents of your Bin folder or modifying web.config file through your code.

Cause 3: Application is hosted in Shared Server or in Web Farm/Garden:
If your application is hosted on a shared server where other applications are also running on the same server then it might be chances that due to other applications IIS is restarted and causing frequent session loss in your application.

Also if your application is hosted in a Web Garden or Web Farm, then you will also notice frequent session loss. E.g. if first request from user is process by server/process 1 and second request is processed by server/process 2, then session will also appear blank.

In both the cases you can go for a seperate state managment.

Resolution:
You can configure a seperate state server, in which session will not lost due to IIS restarts or server/process switching. First configure state server in your machine. For details and configuration of state server see How to Configure Asp.Net State Server.

Also change your web.config file like this:-
<configuration>
  <system.web>
    <sessionState mode="StateServer" cookieless="true" timeout="30"/>
    </sessionState>
  </system.web>
</configuration>

12 comments:

  1. An alternate solution is to use out of process (out-Proc) session caching solution. this way you are not only able to ensure the safety of data, but also utilize the function such as expiration etc. for your session data. For this issue, I used NCache, you can check it out at
    http://www.alachisoft.com/ncache/index.html

    ReplyDelete
  2. I'm getting the same issue. After 5 minutes the session get lost, besides I have already checked all this configurations. This is the code of the .config file



    But after 5 minutes, the session is lost

    ReplyDelete
  3. Try your session timeout settings as mentioned in this link:-

    http://zeeshanumardotnet.blogspot.com/2009/01/why-dot-net-sessions-are-cleared-before.html

    ReplyDelete
  4. Managing both busineѕs hаlvеѕ is іmportant.
    First аnd foгemost, you have to be prepared I was а misfit, сonfesѕes Jаsоn, 39, an еvеnts organіѕer,
    hanԁling party aгrangements.

    Vіsіt mу ωeb page :: dallas internet marketing

    ReplyDelete
  5. Very great post. I just stumbled upon your weblog and wanted to mention that I have truly loved surfing around your blog posts.
    In any case I'll be subscribing to your feed and I'm hoping
    you write again soon!

    Check out my webpage ... Air Jordan

    ReplyDelete
  6. Hi, I log on to уοur blogѕ
    on а гegulаr bаsis.
    Your humorіstic stуle is awesоmе, keep doіng
    whаt yοu're doing!

    My homepage wikisf.ro

    ReplyDelete
  7. Hey there I am so grateful I found your blog page, I
    really found you by mistake, while I was looking on Bing for something
    else, Anyhow I am here now and would just like
    to say thank you for a tremendous post and a
    all round interesting blog (I also love the theme/design),
    I don’t have time to read it all at the minute but I have
    saved it and also included your RSS feeds, so when
    I have time I will be back to read much more, Please do keep up the awesome work.


    my web blog - How to save my Marriage

    ReplyDelete
  8. I savor, cause I found exactly what I was looking for.
    You've ended my four day lengthy hunt! God Bless you man. Have a great day. Bye

    My homepage :: www.mindshake.de

    ReplyDelete
  9. Heya i'm for the primary time here. I found this board and I to find It truly useful & it helped me out a lot. I'm
    hoping to provide something back and aid others such as you aided me.



    Here is my web blog; source

    ReplyDelete
  10. What's up to every one, the contents existing at this web page are genuinely amazing for people experience, well, keep up the nice work fellows.

    Feel free to surf to my site http://www.nenna.at/nahkurse-fur-kids/

    ReplyDelete
  11. A To me, weight loss or just to help kick off their new healthy regime, or
    maybe it is something they do each year. It will vary,
    usually by how much energy many of us should shift our diet into organic or raw
    fruits and vegetables.

    Take a look at my site - www.lerdahl.net

    ReplyDelete
  12. Heya i'm for the primary time here. I came across this board and I to find It really helpful & it helped me out much. I hope to give something back and help others such as you aided me.

    Check out my site ... http://osl2.uca.es/iberogre/index.php/Usuario:ShantellB

    ReplyDelete