Search code examples
.netasp.netappdomain

Are AppDomains are created for every request?


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!


Solution

  • 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.

    Threading and Pooling in the HTTP Pipeline
    (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.