Why do I lose my ASP.NET Sessions due to constant AppDomain’s recyclings?
This might be “old hat” for most of you experienced ASP.NET Developers out there – but I think it is worth another blog entry as I just ran into the following problem:
Step 1: Migrated an Application from .NET 1.1 to .NET 2.0
I worked with an ASP.NET Application originally developed on ASP.NET 1.1. We upgraded to . NET 2.0 using Visual Studio 2008 and the project conversion wizard. Besides some problems with the conversation of project files and web service proxies – that could all be fixed manually – the conversion went smoothly. The application could be started without any problems and seemed to work fine – at least as long as we did not modify the data in the file based data store. All these transactions seemed to cause the loss of all our current ASP.NET Sessions.
Step 2: Debugging through the code
A debugging session showed us that the reason for the lost ASP.NET Sessions was an unexpected recycling of the AppDomain which led to the loss of any currently active sessions. Unfortunately we couldn’t see any reasons why the AppDomain was restarted. There were no severe exceptions nor did anybody change the web.config file on the fly (which would cause a restart).
Step 3: Reading blog entries about AppDomain Recycling
There are a number of blog entries that describe why AppDomain’s might get recycled. And some of them explained a change that happened from .NET 1.1 to .NET 2.0:
- ASP.NET Case Study: Lost session variables and appdomain recycles
- ASP.NET v2.0 – AppDomain recycles, more common than before
Step 4: Identifying our problem
So it turned out that due to the File Change Notifications on the WebRoot directory and all subdirectories AppDomains will now be recycled in case any file in this directory or subdirectory is changed/added/removed. In .NET 1.1 this was not the case – only certain files were checked for modification, e.g.: web.config.
The ASP.NET Application on our hands used a single file based database that was stored in the bin directory of our ASP.NET Application. When changes were commited to the DB and the ADO.NET driver committed those changes to the file system the File Change Notification triggered an AppDomain restart and we lost all of our current ASP.NET Sessions.
Conclusion
Be aware of this change in AppDomain recycling when migrating an older ASP.NET 1.1 Application to ASP.NET 2.0
Related posts:
- ASP.NET MVC: Hidden Performance Problem with HtmlHelper.RenderPartial functions The problem that has been discovered in this blog entry...























Why do I loose my ASP.NET Sessions due to constant AppDomain’s recyclings…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
[...] Here is the original: Why do I loose my ASP.NET Sessions due to constant AppDomain's … [...]
[...] more here: Why do I loose my ASP.NET Sessions due to constant AppDomain's … SHARETHIS.addEntry({ title: "Why do I loose my ASP.NET Sessions due to constant AppDomain's [...]
[...] Why do I loose my ASP.NET Sessions due to constant AppDomain's … [...]
use the Out of Process with the Session State Server to avoid these problems. In the .net 4 time frame you’ll probably do even better by using velocity distributed in memory caching.
You are correct – I could avoid loosing my session by placing them outside of my ASP.NET Process. The general problem however still is that people have to be aware of the fact that there has been a change with AppDomain recycling from ASP.NET 1.1 to 2.0. Changing the session storage to either state server or sql server would not solve the fundamental problem that I ran into.
Thanks for the link to velocity – looking forward to that.
The main reason for ASP.NET Sessions due to constant AppDomain’s recyclings is that ASP.NET’s cache is inproc or in process, which means that the memory used for the cache is allocated from that process’s memory, therefore when the appDomian recycles, the cache is also recycled, which directly means that the ASP.NET sessions and other data in the cache is lost.
To over come this problem, what you can do is to use a out-Proc or out of process cache for your ASP.NET application, for example, I used NCache Express to over come this issue.
you can check it out at
http://www.alachisoft.com/ncache/ncache_express.html
Regards
Max
Yes Max – thats correct – just as I responded to Lynn Eriksen. An out-of-process cache would solve the problem of loosing the ASP.NET Sessions. The general problem that I ran into however still exists – which is the recycling of AppDomains that was caused by a File Change Notification