NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Unloading and Threads

Application domains represent the unit of code unloading in the NGWS runtime. The runtime does not unload code at a more granular level than an application domain, such as by class or by assembly. Each application domain is a process that can be unloaded independently of the other application domains using the Unload method on System.AppDomain.

A new application domain runs on the same thread as the code that created it. There is not a one to one correspondence between threads and application domains. Web server scenarios can require hundreds of application domains per process, whereby the ideal number of threads is some small multiple of the number of processors on the machine.

At any given time, every thread is executing in one application domain. The NGWS runtime keeps track of which threads are running in which application domains. This information, coupled with the fact that direct references are not allowed between domains, allows the runtime to provide clean shutdown of application domains. When a shutdown request is made, the runtime unwinds the thread until it gets to the application domain boundary. At this point, it can "detach" all proxies to objects in other application domains.

Application domains also can each be debugged independently. Stopping at a breakpoint in one application domain will not affect the other application domains in a process. The NGWS debuggers provide a list of currently running application domains and allow the developer to choose which one to debug.