Application domains are usually created and manipulated programmatically by runtime hosts. However, sometimes an application program might also want to work with application domains. For example, an application program could load an application component into a domain to be able to unload the domain (and the component) without having to stop the entire application.
Appdomain 通常由runtime host来创建和操纵。然而, 有时候,应用程序也可能会使用appdomain工作。例如,一个应用程序可以load一个应用组件到一个domain,从而可以在unload这个domain和这个组件,而不用停止整个应用。
You must load an assembly into an application domain before you can execute the code it contains. Running a typical application causes several assemblies to be loaded into an application domain.
在执行一个assembly中包含的code之前,必须先将这个assembly加载到一个application domain中。运行一个典型的应用会使得多个assembly被加载到application domain中。
The way an assembly is loaded determines whether its just-in-time (JIT) compiled code can be shared by multiple application domains in the process, and whether the assembly can be unloaded from the process.
一个assembly被加载的方式决定了是否其JIT编译的code可以被多个application domain共享,以及这个assembly是否可以从进程中被unload。
•If an assembly is loaded domain-neutral, all application domains that share the same security grant set can share the same JIT-compiled code, which reduces the memory required by the application. However, the assembly can never be unloaded from the process.
如果一个assembly被加载为domain-neutral(domain中立),所有的共享相同的安全设置的appdomain可以共享相同的JIT编译的代码,这样可以减少应用的内存占用。但是,这些assembly也就不允许从进程中unload掉;
•If an assembly is not loaded domain-neutral, it must be JIT-compiled in every application domain in which it is loaded. However, the assembly can be unloaded from the process by unloading all the application domains in which it is loaded.
如果一个assemb不是被加载为domain-neutral(domain中立),它就必须在它被加载的那个appdomain中执行JIT编译。 但是,这些assemb可以从process中被unload(通过unload其所在domain来实现);
The runtime host determines whether to load assemblies as domain-neutral when it loads the runtime into a process. For managed applications, apply the LoaderOptimizationAttribute attribute to the entry-point method for the process, and specify a value from the associated LoaderOptimization enumeration. For unmanaged applications that host the common language runtime, specify the appropriate flag when you call the CorBindToRuntimeEx method.
runtime host决定load assembl为domain-neutral,或者不是。对于托管应用,对这个进程的entry point方法应用LoaderOptimizationAttribute 并指定一个相关联的LoaderOptimization枚举中的一个值。对于host 了runtime的非托管应用,调用CorBindToRuntimeEx 方法时,指定一个合适的标志位。
There are three options for loading domain-neutral assemblies:
•SingleDomain loads no assemblies as domain-neutral, except Mscorlib, which is always loaded domain-neutral. This setting is called single domain because it is commonly used when the host is running only a single application in the process.
•MultiDomain loads all assemblies as domain-neutral. Use this setting when there are multiple application domains in the process, all of which run the same code.
•MultiDomainHost loads strong-named assemblies as domain-neutral, if they and all their dependencies have been installed in the global assembly cache. Other assemblies are loaded and JIT-compiled separately for each application domain in which they are loaded, and thus can be unloaded from the process. Use this setting when running more than one application in the same process, or if you have a mixture of assemblies that are shared by many application domains and assemblies that need to be unloaded from the process.
JIT-compiled code cannot be shared for assemblies loaded into the load-from context, using the LoadFrom method of the Assembly class, or loaded from images using overloads of the Load method that specify byte arrays.
【不太理解,load-from context】
Assemblies that have been compiled to native code by using the Native Image Generator (Ngen.exe) can be shared between application domains, if they are loaded domain-neutral the first time they are loaded into a process.
JIT-compiled code for the assembly that contains the application entry point is shared only if all its dependencies can be shared.
A domain-neutral assembly can be JIT-compiled more than once. For example, when the security grant sets of two application domains are different, they cannot share the same JIT-compiled code. However, each copy of the JIT-compiled assembly can be shared with other application domains that have the same grant set.
When you decide whether to load assemblies as domain-neutral, you must make a tradeoff between reducing memory use and other performance factors.
•Access to static data and methods is slower for domain-neutral assemblies because of the need to isolate assemblies. Each application domain that accesses the assembly must have a separate copy of the static data, to prevent references to objects in static fields from crossing domain boundaries. As a result, the runtime contains additional logic to direct a caller to the appropriate copy of the static data or method. This extra logic slows down the call.
•All the dependencies of an assembly must be located and loaded when the assembly is loaded domain-neutral, because a dependency that cannot be loaded domain-neutral prevents the assembly from being loaded domain-neutral.
An application domain forms an isolation boundary for security, versioning, reliability, and unloading of managed code. Threads are the operating system construct used by the common language runtime to execute code. At run time, all managed code is loaded into an application domain and is run by a managed thread.
application domain 形成了一个对安全、版本、可靠性和卸载托管代码的一个边界。线程是操作系统概念,CLR使用线程来执行代码。在运行时,所有托管代码都是被加载到某个application domain,并被某个托管线程执行的。【可以进一步了解一下托管线程】
There is not a one-to-one correlation between application domains and threads. Several threads can be executing in a single application domain at any given time and a particular thread is not confined to a single application domain. That is, threads are free to cross application domain boundaries; a new thread is not created for each application domain.
At any given time, every thread is executing in an application domain. Zero, one, or more than one thread might be executing in any given application domain. The run time keeps track of which threads are running in which application domains. You can locate the domain in which a thread is executing at any time by calling the GetDomain method.