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

C# 使用单独AppDomain的单元测试代码,c#,visual-studio,unit-testing,visual-studio-2017,appdomain,C#,Visual Studio,Unit Testing,Visual Studio 2017,Appdomain,我有使用AppDomain动态加载插件的代码。我有一个名为PluginFinder的私有内部类来完成这项工作。这一切都可以在生产环境中工作,但我现在正试图为它编写MS单元测试,我收到一个错误,说找不到程序集 需要说明的是,main类创建AppDomain并在其中运行PluginFinder,以加载使用正确接口找到的类。加载类后,控件将返回默认的AppDomain。PluginFinder仅在加载期间使用 我使用以下代码创建环境: // Create a domain t

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

需要说明的是,main类创建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 ----
我已经找到了解决方案,这表明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);

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

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

我相信这是因为用于探测的基本目录没有使用该重载正确设置。使用如下方式获取AppDomainSetup的重载:
AppDomain.CreateDomain(“PlugInLoader”,null,AppDomain.CurrentDomain.SetupInformation)@MikeZboray-这就是问题所在。如果你发布上述内容,我会将其标记为解决方案。我在问题中添加了代码以显示所做的更改。非常感谢。我相信这是因为用于探测的基本目录没有使用该重载正确设置。使用如下方式获取AppDomainSetup的重载:
AppDomain.CreateDomain(“PlugInLoader”,null,AppDomain.CurrentDomain.SetupInformation)@MikeZboray-这就是问题所在。如果你发布上述内容,我会将其标记为解决方案。我在问题中添加了代码以显示所做的更改。非常感谢。