Free mag vol1 | Page 693

CHAPTER 17  PROCESSES, APPDOMAINS, AND OBJECT CONTEXTS • AppDomains provide a deeper level of isolation for hosting a loaded application. If one AppDomain within a process fails, the remaining AppDomains remain functional. As mentioned, a single process can host any number of AppDomains, each of which is fully and completely isolated from other AppDomains within this process (or any other process). Given this fact, be very aware that an application running in one AppDomain is unable to obtain data of any kind (global variables or static fields) within another AppDomain, unless they make use of a distributed programming protocol (such as Windows Communication Foundation). While a single process may host multiple AppDomains, this is not typically the case. At the very least, an OS process will host what is termed the default application domain. This specific application domain is automatically created by the CLR at the time the process launches. After this point, the CLR creates additional application domains on an as-needed basis. The System.AppDomain Class The .NET platform allows you to programmatically monitor AppDomains, create new AppDomains (or unload them) at runtime, load assemblies into AppDomains, and a whole slew of additional tasks, using the AppDomain class in the System namespace of mscorlib.dll. Table 17-5 documents some useful methods of the AppDomain class (consult the .NET Framework 4.5 SDK documentation for full details). Table 17-5. Select Methods of AppDomain 636 Method Meaning in Life CreateDomain() This static method allows you to create a new AppDomain in the current process. CreateInstance() This creates an instance of a type in an external assembly, after loading said assembly into the calling application domain. ExecuteAssembly() This method executes an *.exe assembly within an application domain, given its file name. GetAssemblies() This method gets the set of .NET assemblies that have been loaded into this application domain (COM-based or C-based binaries are ignored). GetCurrentThreadId() This static method returns the ID of the active thread in the current application domain. Load() This method is used to dynamically load an assembly into the current application domain. Unload() This is another static method that allows you to unload a specified AppDomain within a given process.