Introduction to AppDomains
Whereas the assembly is a logical unit of deployment, the AppDomain is a logical unit of execution. The AppDomain is a sandbox in which .NET code runs. The AppDomain provides a container in which code can execute safely, knowing that code running outside the AppDomain cannot negatively impact it. As you will see throughout this book, cross-process communication done with Remoting is actually the process of passing information between AppDomains, whether they are on the same machine or different machines.
Throughout the course of an application, your code may load a lot of assemblies creating instances of other classes. Without the use of the AppDomain class, all of those assemblies will remain in memory for the lifespan of your application. If you are coding a long-running server application, this is unacceptable. Using the AppDomain class, however, you can manually control the loading and unloading of assemblies, as well as create an isolated environment to run potentially volatile tasks.
The AppDomain class represents the properties and functionalities of this memory sandbox, as shown in Table 12.1.
Table 12.1. AppDomain Properties and Methods
Gets the identity of the application in the AppDomain, containing information such as the location and full name.
The base directory that will be used to look for dependent assemblies.
Gets an instance of the AppDomain class, indicating the current AppDomain.
Gets the friendly name of the application domain.
Gets an AppDomainSetup instance containing application and configuration binding data.
Creates an instance of a COM object. You will learn more about this in Chapter 13.
Creates a new named AppDomain.
Creates an instance of a specified type in the specified assembly.
Creates an instance of the specified type in the specified assembly file (requires the .dll or .exe extension).
Executes the assembly file specified, such as another .exe.
Obtains the list of assemblies loaded within the AppDomain.
Gets data contained in a named slot visible to all assemblies in the AppDomain.
Loads an assembly into the AppDomain.
Sets data contained in a named slot visible to all assemblies in the AppDomain.
Sets the principal (security user) that will be presented when a thread is queried for the security context.
Unloads an AppDomain and releases all loaded assemblies.