IIS 7 and Above
Single Application Domain for an IIS app
Last post Apr 12, 2018 05:04 PM by lextm
Apr 11, 2018 09:55 AM|Jakub.Ledworowski|LINK
I have an app hosted on IIS 8.5 (in separate ApplicationPool) which can only run in
single AppDomain because it loads native dll.
Yet, when the content of the bin/ folder has changed, IIS creates new AppDomain to process incoming requests, while the old AppDomain is being Disposed. And this is not acceptable.
I have set Disable Overlapped Recycle to True, and Maximum Worker Processes to 1, but this does not help.
How to configure the app (or IIS) to be sure that only one AppDomain at a time is alive?
Apr 12, 2018 02:44 AM|Yuk Ding|LINK
What do you mean "this does not help"? You mean the recycle will create a new pool with old pool side by side? If you have enabled the disable overlapped recycle, there should always only one pool. Based on my understanding, the Appdomain save the session
in in-proc mode. Recycle application pool will always lose all session.
I think you should keep the application pool always on in this case. You need to set the time interval and recycle interval to 0.
Then manual recycle the application pool when you face a bug.
Apr 12, 2018 09:37 AM|Jakub.Ledworowski|LINK
Hi Yuk Ding,
Suppose that new request are coming constantly to the application. I modify the bin/ content or web.config etc., and IIS performs recycle of the application. Too see what happens, please find the logs below, where I printed the
[ManagedThreadId] [AppDomain-ThreadId] [AppDomain-FriendlyName] [AppDomain-DomainId]:
04/12/2018 10:48:00.162   [/LM/W3SVC/1/ROOT/TestApplication-1-131679964648270863]  DEBUG: Finalize Start
04/12/2018 10:48:01.346   [/LM/W3SVC/1/ROOT/TestApplication-2-131679964803559320]  DEBUG: Initialize Start
04/12/2018 10:48:02.162   [/LM/W3SVC/1/ROOT/TestApplication-1-131679964648270863]  DEBUG: Finalize End
So, as you see, AppDomain TestApplication-1 was starting to being disposed by IIS (Finalize Start). It takes a few seconds to perform all dispose operations. In the middle of this Finalize, the second AppDomain
TestApplication-2 is started by IIS and performs initialization (Initialize Start), before the first AppDomain finished the Finalize. This is what's causing a fail. I'd like to firstly fully dispose of the first
AppDomain, before another one can start.
Note that these two AppDomains run under
the same w3wp.exe process, so from the Disable Overlapped Recycle point of view it is OK, because only single process is held for this particular application pool.
Apr 12, 2018 05:04 PM|lextm|LINK
IIS performs recycle of the application.
That's a misunderstanding. What you observed is ASP.NET reloading the application. Thus, it is not application pool recycle, and all you previous changes to IIS configuration won't take any effect.
There does not seem to be an ASP.NET setting to change the behavior of old/new AppDomain objects as you wished (but you can search further to confirm, like the reference code of ASP.NET on GitHub). But I wonder if you really need that.
For example, by adding some code to your application, you can easily prevent the new AppDomain from loading the native library.
Of course it requires code change, but you no longer care about how ASP.NET works internally.