Recycling AppDomains
page 1 of 1
Published: 17 Nov 2003
Unedited - Community Contributed
This article explains the concept of an AppDomain and provides details on a custom .Net console application to selectively recycle them on a web server.
by Robert Boedigheimer
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 11250/ 8


This article details how to selectively recycle AppDomains in ASP.NET 1.x sites.  ASP.NET web sites are executed in the context of a worker process.  IIS 5 uses a single worker process (aspnet_wp.exe) for all web sites (assuming no web gardens) and IIS 6 users a worker process (w3wp.exe) for each application pool.  In order to provide separation between web sites that share a worker process, ASP.NET uses a new .NET concept called an AppDomain.  The AppDomain is a logical process that exists inside a worker process to provide isolation.  ASP.NET uses an AppDomain for each ASP.NET web site.  As an example, if you use IIS 5 with 3 ASP.NET web sites there will be a single worker process that contains 3 AppDomains.  The AppDomain stores the Application, Cache, and in-process Session objects for a site.  Sometimes it is necessary to recycle one of these AppDomains, preferrably without having to restart the worker process which would affect other AppDomains. 

Why Recycle?

Sometimes it is necessary to recycle a single AppDomain without impacting other ASP.NET sites on the same web server.  One example of this is when a web site uses a shared assembly (strong named assembly deployed to the Global Assembly Cache (GAC)) that has been updated.  When an application starts, the Common Language Runtime (CLR) binds to the the specific version of each assembly referenced in a project by default, but this can be altered through the use of configuration files or publisher policy files.  If a developer modifies a shared assembly and moves it to the GAC on the web server, the CLR will not start to use the new assembly even if configured to do so because once an application binds to an assembly it does not look for new updates each time it is used (for performance reasons). 

So how does the web site start to use the new assembly?  The AppDomain needs to be restarted.  It is possible to restart IIS or kill the worker process to get this to occur, but that is like using a sledgehammer to hang a picture--it works but does unneccesary damage to the surroundings.

Recycling an AppDomain

The method used here is to "touch" config files (machine.config or web.config) which will cause the AppDomain to recycle.  The touch is just an update of the last time the file was written without changing the file contents.  Touching the machine.config file will recycle all AppDomains on a server (for the specific version of .NET installed with that machine.config), while touching a web.config will just recycle that specific web site's AppDomain.  

A zip file is included that contains the source for the .NET console application that provides the touch functionality.  The program xx055.exe accepts paths to machine.config files (may be more than one if have more than one version of .NET installed), paths to web.config files, and other file system directory paths.  It will touch each specific config file provided.  When given a directory path, it will recursively touch a web.config file in that directory and all of its subdirectories.  The program by default logs an entry to the event log that details which files were touched.  This technique requires that the web.config files be writable so the last write time can be altered.  Warning:  Recycling an AppDomain will drop Application, Cache, and in-process Session information since that is stored in the AppDomain logical process! 


The ability to recycle a specific AppDomain for an ASP.NET web site provides a method to very selectively target a web site for restarts.  There are many times where this may prove useful, such as when shared assembly updates need to be loaded.  By "touching" the appropriate config files, the desired AppDomains can be restarted.

Send comments or questions to

User Comments

Title: Re: Sudarshan   
Name: Robert Boedigheimer
Date: 2012-05-18 8:56:39 PM
I know that the AppDomain class has an Unload() method. I would also try to modify its config file and see if that works.
Title: Ms   
Name: Sudarshan
Date: 2012-05-16 10:03:52 AM
Good information regarding the AppDomain, i wanted to ask one more thing, how do we implement this in Windows form application?? Any ideas are Welcomed!!
Title: Ms   
Name: J.VijeyaShobana
Date: 2008-06-10 2:14:09 PM
Good article. useful.
Title: Re: Rob Betz   
Name: Robert Boedigheimer
Date: 2005-07-20 7:45:58 AM
In both IIS 5 and IIS 6 if you are using the default in-process storage for session, it will be lost when the AppDomain is recycled. IIS 6 does spawn a new worker process but it does not do any transfer of existing memory like cache or in-process session. Using the state service or SQL Server for session storage would avoid that problem.
Title: Developer   
Name: Rob Betz
Date: 2005-07-19 4:53:09 PM
If you are running IIS 6 will you lose session if you recycle app? There is a setting to recycle, it will open a new worker process and transfer from old to new, but does this transfer the session state. Please advise.

Title: Mr   
Date: 2004-07-02 1:27:56 AM

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-15 10:06:12 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search