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.

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();

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.

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:-
    <sessionState mode="StateServer" cookieless="true" timeout="30"/>