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!
AppDomains are created by the . Net runtime when a managed application is initialised. When you start ABC. EXE, it gets an application domain.
Process A runs managed code with one application domain while Process B runs managed code has three application domains. Note that Process C which runs unmanaged code has no application domain. Code and data are safely isolated using the boundary provided by the AppDomain.
An application domain (commonly called AppDomains) is a logical unit of isolation that enables you to execute multiple applications within the same process while at the same time ensuring that crash of a particular application domain doesn't affect the functioning of another application domain.
The AppDomain class implements a set of events that enable applications to respond when an assembly is loaded, when an application domain will be unloaded, or when an unhandled exception is thrown. For more information on using application domains, see Application Domains.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With