Unit Testing code that uses a separate AppDomain

Unit Testing code that uses a separate AppDomain

I have code that uses an AppDomain to dynamically load plugins. I have a private internal class called PluginFinder to do the work. This all works in the production environment, but I am now trying to write MS Unit Tests for it, and I am getting an error saying the assembly could not be found.

I have code that uses an AppDomain to dynamically load plugins. I have a private internal class called PluginFinder to do the work. This all works in the production environment, but I am now trying to write MS Unit Tests for it, and I am getting an error saying the assembly could not be found.

To be clear, the main class creates the AppDomain and runs the PluginFinder in it to load classes it finds with the correct Interface. Once the classes are loaded, control returns to the default AppDomain. The PluginFinder is only used during loading.

I use the code below to create the environment:

            //  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 uses a default constructor, and is a private internal class. The exception I get is below:

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

I have found this solution, which suggests that NUnit runs in multiple AppDomains, which makes testing of AppDomains impossible without plugins. I'm using MS Unit Test, but I suspect that this may be the case here as well.

** RESOLVED **

As was mentioned in the comment below, I needed to use the overload constructor. I modified my code (shown below), and it worked.

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


c-sharp visual-studio

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Level-up your C++ productivity using Visual Studio

Come learn how you can improve your efficiency even while working from home. We’ll take a look at online development environments, Live Share, and a host of other new productivity features and tips.

Install Visual Studio 2019 Community – Best IDE for C#

Here is a straightforward tutorial on how to install Visual Studio 2019 Community onto Windows 10. Visual Studio 2019 is probably the best IDE ever for C# devs.

Modern C++ development with Visual Studio

Join us for a demo packed session on the latest improvements in Visual Studio 2019, including new C++20 features, cross-platform CMake integration, and support for Visual Studio Codespaces.

C++ Development with Visual Studio Code

If you’re looking for a fast and lightweight open-source code editor, Visual Studio Code has you covered.

Pixel Color Bot in C#, Visual Studio, .NET and WPF

Create a Pixel Color Bot in C# Visual Studio by using the .NET framework and WPF. This C# tutorial guides you through the development process of creating a pixel bot.