Summary
Application domains are often described as subprocesses because they provide an isolation boundary around a grouping of assemblies in a process. If the code running in the application domain is verifiably type safe, the isolation provided by the domain is
When writing an extensible application, you'll likely want to take advantage of a new concept in .NET Framework version 2.0 called an application domain manager. Providing an application domain manager is an easy way to implement the host runtime design pattern because the CLR automatically creates an instance of your domain manager in every application domain. Also, if you're writing a CLR host, an application domain manager is the mechanism you use to call into managed code from the unmanaged portion of your host. |
Chapter 6. Configuring Application DomainsIn Chapter 5, I describe application domains as a construct used to isolate groups of assemblies that are loaded into the same process. An important aspect of this isolation is the ability for the creator of an application domain to be able to constrain various aspects of how the domain operates. For example, a domain's creator must be able to limit the locations from which unsigned assemblies can be loaded into the domain. Other examples include the ability to control how version policy applies to assemblies in the domain and to specify the configuration file where application-specific settings can be stored. These aspects of application domain behavior, along with many others, can be customized through various settings specified when the domain is created. In this chapter, I describe how you can use these settings to customize the behavior of application domains to fit your particular scenario. Application domain managers also play a key role in your ability to customize how application domains are created in the process. Recall from the previous chapter that an instance of your application domain manager is created by the CLR in every application domain. As part of this process, the CLR calls your application domain manager both before and after a new domain has been created to give you a chance to configure it. This detail is important because it gives you, the author of an extensible application, the ability to decide how all domains are created in the process, including those created by any add-ins you have loaded, not just the ones you create yourself. After I describe the full set of application domain configuration settings, I explain your domain manager's role in customizing how application domains are created in a process. |
Application Domain Configuration SettingsThe complete list of application domain configuration settings is shown here. These settings are manipulated programmatically through public properties on the System.AppDomainSetup object. ApplicationBase PrivateBinPath DisallowApplicationBaseProbing PrivateBinPathProbe DisallowPublisherPolicy DisallowBindingRedirects ConfigurationFile LicenseFile AppDomainInitializer ConfigurationBytes ShadowCopyFiles ShadowCopyDirectories CachePath DynamicBase ApplicationName LoaderOptimization DisallowCodeDownload ActivationArguments AppDomainInitializerArguments
The
ConfigurationBytes
"property" is actually implemented by two
Before discussing how to use AppDomainSetup , take a look at the general categories of settings you can apply to an application domain:
Typically, the settings used to configure an application domain are specified when the domain is created. This is done by creating a new instance of System.AppDomainSetup , providing values for the properties you're interested in, and passing the new instance to the System.AppDomain.CreateDomain method. For example, the following code specifies the base directory in which the CLR will search for private assemblies for the new domain:
using System;
AppDomainSetup adSetup = new AppDomainSetup();
adSetup.ApplicationBase = @"c:\Program Files\MyApp";
AppDomain ad = AppDomain.CreateDomain("New Domain", null, adSetup);
Any time after the domain has been created, you can retrieve the current settings by obtaining an instance of AppDomainSetup using the SetupInformation property of System.AppDomain . For example, the following code prints the application root directory for the application domain running on the current thread:
using System;
AppDomainSetup adSetup = Thread.GetDomain().SetupInformation;
Console.WriteLine("The application base directory for the current
Domain is: " + adSetup.ApplicationBase);
As I explained earlier, you typically specify the configuration settings for an application domain when you create the domain. However, System.AppDomain exposes public methods that enable you to change the value of a small number of these settings after the domain has been created. If you use these methods to change domain settings, you must do so before any assemblies are loaded into the domain. Calling them later will have no effect. The settings that can be changed through properties on AppDomain , along with the corresponding properties on AppDomainSetup they affect, are shown in Table 6-1. Table 6-1. Application Domain Settings Exposed by System.AppDomain
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %} I describe how to use these methods in the following sections, where their matching AppDomainSetup property is described. Now that I've provided an overview of each of the settings, let's dig into the details of how you can use the AppDomainSetup properties to customize the application domains you create. |