如何从GAC卸载程序集?

时间:2009-11-09 09:28:04

标签: .net assemblies clr appdomain

我正在尝试对特定产品进行自动化测试。 测试包括将产品安装到硬盘驱动器上的不同位置,然后对其执行一些操作,然后关闭应用程序。

启动流程的代码如下所示:

using (Process process = new Process())
            {
                process.StartInfo.FileName = "C:\mylocation\myapp.exe";
                process.Start();
            }

在连续执行测试时,当应用程序的安装位置发生变化时,我从上面的代码中得到一个例外:

  

API限制:程序集   '文件:/// C:\ alternate_location \ MyApp.exe的'   已经从另一个加载   地点。它不能从一个加载   新的位置在同一个   应用程序域。

由于这个原因,测试不能连续运行。

可以做些什么来克服这个问题?无论如何我可以从GAC卸载组件吗?

我可以在我的测试应用程序中做些什么来克服这个问题吗?或者在我正在测试的应用程序中是否需要更改某些内容?

3 个答案:

答案 0 :(得分:1)

加载后,无法从应用程序域卸载程序集。但是您可以创建一个新的应用程序域(AppDomain class),在其中加载程序集,使用它们,然后卸载域。见这里:Good example of use of AppDomain

答案 1 :(得分:1)

向GAC添加内容并不是组件定义的固有部分 - 通常由安装程序等完成。

可以使用gacutil工具从GAC中删除您的工具。在1.1中,它在框架目录中。在较新的版本中,它位于SDK中,例如C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin

答案 2 :(得分:0)

您能为我们提供更多信息吗?我无法重现此错误。

Process.Start应该使用自己的AppDomain创建一个新进程。

在我的机器上,我创建了一个项目Harness,它有一个对DoNothing的项目引用,它是一个强签名程序集和一个对LaodDoNothing的项目引用,它引用了c:\ DoNothing.exe。我已经将下面的代码粘贴在Harness.Main中,并将调试输出作为内联注释。带有unsigned后缀的exes没有签名。

//debug outputs when Main is jitted:'Harness.vshost.exe' (Managed): Loaded 'c:\project\DoNothing\Harness\bin\Debug\DoNothing.exe', Symbols loaded.
//debug outputs when Main is jitted:'Harness.vshost.exe' (Managed): Loaded 'c:\project\DoNothing\Harness\bin\Debug\LoadDoNothing.exe', Symbols loaded.

ZaZaZa.Main();
LoadDoNothing.Program.Main();
using (Process process = new Process())
{
    process.StartInfo.FileName = @"C:\donothingunsigned.exe";
    process.Start(); //debug outputs The thread 0x17f0 has exited with code 0 (0x0).  No assemblies loads are logged to debug because this is a separate process.


}

using (Process process = new Process())
{
    process.StartInfo.FileName = @"C:\3\donothingunsigned2.exe";
    process.Start(); //Debug outputs The thread 0x1014 has exited with code 0 (0x0). No assemblies loads are logged to debug because this is a separate process.
}
AppDomain.CurrentDomain.ExecuteAssembly(@"C:\donothingunsigned.exe"); //debug outputs 'Harness.vshost.exe' (Managed): Loaded 'C:\donothingunsigned.exe'
AppDomain.CurrentDomain.ExecuteAssembly(@"C:\3\donothingunsigned2.exe"); //no debug output because the loader realizes this assembly has already been loaded and uses that.