在共享应用程序域中包含的是与应用程序域无关的代码。mscorlib.dll将被加载到这个应用程序域中(由系统应用程序域负责加载),此外还包括在System命名空间中的一些基本类型(例如String、enum、ValueType、Array等)。在大多数情况下,非用户代码(non-user code)将被加载到共享应用程序域中,不过也有一些机制可以将用户代码(user code)加载到共享应用程序域中。启用了CLR的应用程序域可以通过加载器的优化属性来注入用户代码。
默认应用程序域通常,.NET程序在默认应用程序域中运行。位于默认应用程序域中的所有代码都只有在这个域中才是有效的。由于应用程序域实现了一种有逻辑并且可靠的边界,因此任何跨越应用程序域的访问操作都必须通过.NET远程对象来进行。
四、动态创建、卸载应用程序域前面我们看到程序启动时,系统帮我们创建的默认应用程序域,然后手动创建应用程序域也是可以的,代码如下:
using System; namespace Sample02 { class Program { static void Main(string[] args) { Console.WriteLine("Before CreateDomain"); Console.ReadLine(); AppDomain domain = AppDomain.CreateDomain("NewDomain"); Console.WriteLine("After CreateDomain, Before Unload"); Console.ReadLine(); AppDomain.Unload(domain); Console.WriteLine("After Unload"); Console.ReadLine(); } } }程序开始运行时:
有三个应用程序域:System Domain、Shared Domain、Domain 1
0:006> !DumpDomain -------------------------------------- System Domain: 7230d5a8 LowFrequencyHeap: 7230d8cc HighFrequencyHeap: 7230d918 StubHeap: 7230d964 Stage: OPEN Name: None -------------------------------------- Shared Domain: 7230d258 LowFrequencyHeap: 7230d8cc HighFrequencyHeap: 7230d918 StubHeap: 7230d964 Stage: OPEN Name: None Assembly: 00df8468 [C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll] ClassLoader: 00df8520 Module Name 6a681000 C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll -------------------------------------- Domain 1: 00dd7da0 LowFrequencyHeap: 00dd820c HighFrequencyHeap: 00dd8258 StubHeap: 00dd82a4 Stage: OPEN SecurityDescriptor: 00dd0120 Name: Sample02.exe Assembly: 00df8468 [C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll] ClassLoader: 00df8520 SecurityDescriptor: 00dfc018 Module Name 6a681000 C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll Assembly: 00dff8d8 [E:\Workspace\DotNet\WinDbgInspectCLR\Sample02\bin\Debug\Sample02.exe] ClassLoader: 00dff318 SecurityDescriptor: 00dfcd60 Module Name 00ec401c E:\Workspace\DotNet\WinDbgInspectCLR\Sample02\bin\Debug\Sample02.exe按Enter继续执行,多一个应用程序域Domain 2
0:006> !DumpDomain The version of SOS does not match the version of CLR you are debugging. Please load the matching version of SOS for the version of CLR you are debugging. CLR Version: 4.7.3190.0 SOS Version: 4.6.1648.0 -------------------------------------- System Domain: 7230d5a8 LowFrequencyHeap: 7230d8cc HighFrequencyHeap: 7230d918 StubHeap: 7230d964 Stage: OPEN Name: None -------------------------------------- Shared Domain: 7230d258 LowFrequencyHeap: 7230d8cc HighFrequencyHeap: 7230d918 StubHeap: 7230d964 Stage: OPEN Name: None Assembly: 00df8468 [C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll] ClassLoader: 00df8520 Module Name 6a681000 C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll -------------------------------------- Domain 1: 00dd7da0 LowFrequencyHeap: 00dd820c HighFrequencyHeap: 00dd8258 StubHeap: 00dd82a4 Stage: OPEN SecurityDescriptor: 00dd0120 Name: Sample02.exe Assembly: 00df8468 [C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll] ClassLoader: 00df8520 SecurityDescriptor: 00dfc018 Module Name 6a681000 C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll Assembly: 00dff8d8 [E:\Workspace\DotNet\WinDbgInspectCLR\Sample02\bin\Debug\Sample02.exe] ClassLoader: 00dff318 SecurityDescriptor: 00dfcd60 Module Name 00ec401c E:\Workspace\DotNet\WinDbgInspectCLR\Sample02\bin\Debug\Sample02.exe -------------------------------------- Domain 2: 00e0cf00 LowFrequencyHeap: 00e0d36c HighFrequencyHeap: 00e0d3b8 StubHeap: 00e0d404 Stage: OPEN SecurityDescriptor: 00e09fe0 Name: NewDomain Assembly: 00df8468 [C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll] ClassLoader: 00df8520 SecurityDescriptor: 00dfccd8 Module Name 6a681000 C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll Assembly: 00e17398 [E:\Workspace\DotNet\WinDbgInspectCLR\Sample01\bin\Debug\Sample01.exe] ClassLoader: 00e21d90 SecurityDescriptor: 00dfc128 Module Name 011949f4 E:\Workspace\DotNet\WinDbgInspectCLR\Sample01\bin\Debug\Sample01.exe