我想使用AppDomain加载和卸载程序集,但它不起作用。 这是我提出的最简单的例子。
using System;
using System.Reflection;
namespace DomainTest
{
class Program
{
static void Main(string[] args)
{
AppDomain _newDomain;
var setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.ApplicationName = "Isolator";
_newDomain = AppDomain.CreateDomain("Isolation:" + Guid.NewGuid(),
null, setup);
Console.WriteLine("-----------------Before Domain created");
ShowLoadedAssemblies();
_newDomain.Load("WorkerTest");
Console.WriteLine("---------------------After Load");
ShowLoadedAssemblies();
AppDomain.Unload(_newDomain);
Console.WriteLine("--------------------After Unload");
ShowLoadedAssemblies();
Console.ReadLine();
}
public static void ShowLoadedAssemblies()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
//Make an array for the list of assemblies.
Assembly[] assems = currentDomain.GetAssemblies();
//List the assemblies in the current application domain.
Console.WriteLine("Currently loaded Assemblies:");
foreach (Assembly assem in assems)
Console.WriteLine(assem.ToString());
}
}
}
为了简化示例,WorkerTest程序集是一个空程序集 除了标准的Microsoft.CSharp和System.Dll
之外没有任何引用namespace WorkerTest
{
public class Worker
{
}
}
此示例有一个带有Main()方法的Program类,我在其中创建一个新的AppDomain(_newDomain)并将WorkerTest.DLL程序集加载到其中。之后,我卸载_newDomain并期望使用它卸载WorkerTest.DLL,但它不会发生。下面您可以看到程序输出,它显示在加载之前,在将WorkerTest加载到_newDomain之后和卸载_newDomain之后加载的所有程序集。 WorkerTest.DLL仍然被加载。 有人能告诉我为什么它不起作用吗?
-----------------Before Domain created
Currently loaded Assemblies:
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Microsoft.VisualStudio.HostingProcess.Utilities, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.HostingProcess.Utilities.Sync, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.Debugger.Runtime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
vshost32, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
DomainTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
---------------------After Load
Currently loaded Assemblies:
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Microsoft.VisualStudio.HostingProcess.Utilities, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.HostingProcess.Utilities.Sync, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.Debugger.Runtime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
vshost32, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
DomainTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WorkerTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
--------------------After Unload
Currently loaded Assemblies:
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Microsoft.VisualStudio.HostingProcess.Utilities, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.HostingProcess.Utilities.Sync, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.Debugger.Runtime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
vshost32, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
DomainTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WorkerTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
答案 0 :(得分:0)
我认为问题在于_newDomain.Load("WorkerTest");
将Assembly
对象返回到当前应用域。由于Assembly
包含对所有元信息的访问权限,因此它必须实际将程序集加载到当前域。
要将程序集正确加载到新域中,应该仅通过该新域中的代码来完成。或者,如果您只需要创建对象的实例 - Loading DLLs into a separate AppDomain