Reputation: 40573
In ASP.NET 3.5 (with IIS6), are AppDomains are created for every request? I know that all applications have their own AppDomain under w3wp.exe, but how exactly does the whole AppDomain work?
I was arguing today with a colleague who was trying to convince me that if an ASP.NET application has a static object (or Singleton class), that this object will be shared among all the requests. I think this is false. Am I right? How do I convince my colleague?
Thanks!
Upvotes: 8
Views: 2746
Reputation: 1081
I'm sorry to say that your colleague is correct. Within an ASP.NET application, each application configured as such in IIS runs within its own AppDomain, which is the scope of a singleton object. So a singleton in App1 is available to all requests to App1 (and could become a concurrency if not handled carefully), but requests in App2 would not be able to access the singleton in App1.
(source: microsoft.com)
This diagram from MSDN Magazine helps show how each application is isolated in its own AppDomain. While the diagram shows an IIS5 worker process (aspnet_wp.exe), an IIS6 worker process would be similar for applications configured to run in the same Application Pool.
Upvotes: 9
Reputation: 18792
A singleton will exist in the entire scope of the appdomain. Also, all of the requests to your application will go to the same appdomain, so your colleague is actually correct.
Update: The question spurred by curiosity and I found a "singleton" that you can use on a per-request basis. I don't actually recommend that though. If someone needs a per-request singleton then they need to find a more fitting pattern.
Upvotes: 1