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

时间:2019-02-11 19:10:30

标签: c# visual-studio unit-testing visual-studio-2017 appdomain

我有使用AppDomain动态加载插件的代码。我有一个名为PluginFinder的内部私有类来完成这项工作。这一切都可以在生产环境中使用,但是我现在正尝试为其编写MS单元测试,并且出现一个错误,提示找不到装配。

要清楚,主类创建AppDomain并在其中运行PluginFinder,以使用正确的接口加载找到的类。加载类后,控件将返回默认的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使用默认构造函数,并且是私有内部类。我得到的异常如下:

    ------ 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);

1 个答案:

答案 0 :(得分:1)

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

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