使用单独 AppDomain 的单元测试代码

Unit Testing code that uses a separate AppDomain

我有使用 AppDomain 动态加载插件的代码。我有一个名为 PluginFinder 的私有内部 class 来完成这项工作。这一切都在生产环境中工作,但我现在正在尝试为其编写 MS 单元测试,但我收到一条错误消息,提示找不到程序集。

明确地说,主要 class 创建 AppDomain 并在其中运行 PluginFinder 以加载它使用正确接口找到的 classes。加载 classes 后,将 returns 控制到默认的 AppDomain。 PluginFinder 仅在加载期间使用。

我使用下面的代码创建环境:

            //  Create a domain to text for plugins
            AppDomain domain = AppDomain.CreateDomain("PluginLoader");

            //  Set PluginFinder to run in the new domain (assemblyName, typeName)
            (PluginFinder)domain.CreateInstanceAndUnwrap(
                typeof(PluginFinder).Assembly.FullName, typeof(PluginFinder).FullName);

PluginFinder 使用默认构造函数,并且是私有内部 class。我得到的异常如下:

    ------ Exception ------
Error:  Could not load file or assembly 'MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
There was an error creating the Plugin Loader.
Could not load file or assembly 'MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(String assemblyName, String typeName)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
   at MyClass.loadPlugins() in C:\Code\...
---- End Exception ----

我找到了 this 解决方案,这表明 NUnit 在多个 AppDomain 中运行,这使得在没有插件的情况下无法测试 AppDomain。我正在使用 MS 单元测试,但我怀疑这里也可能是这种情况。

**** 已解决 ****

正如下面评论中提到的,我需要使用重载构造函数。我修改了我的代码(如下所示),它成功了。

// Set up the AppDomainSetup
var setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;

// Set up the Evidence
var baseEvidence = AppDomain.CurrentDomain.Evidence;

//  Create a domain to text for plugins
AppDomain domain = AppDomain.CreateDomain("PluginLoader", baseEvidence, setup);

//  Set PluginFinder to run in the new domain (assemblyName, typeName)
PluginFinder finder = (PluginFinder)domain.CreateInstanceAndUnwrap(
    typeof(PluginFinder).Assembly.FullName, typeof(PluginFinder).FullName);

我不太确定 NUnit 在这里做什么,但问题可能是新的 AppDomain 的基目录不是您所期望的。考虑使用父域的设置信息或在创建新域时直接从当前域显式复制基础:

AppDomain.CreateDomain("PlugInLoader", null, AppDomain.CurrentDomain.SetupInformation);