Double App Domains in ASP.NET 4 application

.net-4.0, appdomain, asp.net, iis-7

Solution

Even though you aren't creating an `AppDomain`, a library that you are using might be. What third-party components are you using? Do you have any Inversion of Control or Dynamic Proxy libraries that might be responsible? Here's an explanation of this happening with Castle.

Are you sure the application is only running in one place in IIS? It's possible to have multiple IIS sites/applications running off of the same files. This would be consistent with (1) getting your debug info from the db, rather than the app, and (2) the recycle due to editing `web.config` consistently resulting in duplicate domains. If one location is more commonly accessed than the other this could explain why there is sometimes only one `AppDomain`.

If you are leveraging ASP.NET's dynamic compilation and shadow copying feature, ASP.NET will at times have multiple `AppDomain`s. Jim Schubert wrote an article called ASP.NET, AppDomains, and shadow-copying which explains this in more detail as well as makes several suggestions as to how to modify `web.config` to customize this. He also has a helpful answer over at Does my ASP.NET application stop executing if I overwrite the DLLs? Shadow copying can be disabled by setting `<hostingEnvironment shadowCopyBinAssemblies="false" />`.

Update

I got sucked into Jim Schubert's blog and ended up reading this unrelated post on Allowing Only A Single Instance of a .NET application. If all else fails, you could use this approach to ensure only one instance of the application is running.

Problem

I've got an ASP.NET application running on IIS 7 with multiple application domains, and I can't fathom why there are multiple app domains in a single process. I've grepped my code base, and I'm not explicitly creating a second application domain. Is it possible that a recycle has failed to time out? - These double domains will persist for sometime. - If a recycle occurs because of a web config or binary change, both app domains will go down, and two new ones will start up. - These servers are subject to several binary patches and IISResets per day - sometimes there are 2 domains, sometimes only 1. - Web gardening is disabled. - I discovered this because there is a timer in the application heart-beating to the database, and noticed one day the server had two heartbeats. In windbg, !dumpdomain shows me the following result: (filtered to only show names of app domains): ``` Line 59: Name: None Line 66: Name: None Line 372: Name: DefaultDomain Line 460: Name: /LM/W3SVC/1/ROOT/MyAppDomain-1-129882892717131250 Line 4437: Name: /LM/W3SVC/1/ROOT/MyAppDomain-4-129285605131450579 ```

Original source

Related problems