diff --git a/src/NUnitEngine/nunit.engine.core/Drivers/NUnitNetCore31Driver.cs b/src/NUnitEngine/nunit.engine.core/Drivers/NUnitNetCore31Driver.cs index 53deab311..8d45c4ee5 100644 --- a/src/NUnitEngine/nunit.engine.core/Drivers/NUnitNetCore31Driver.cs +++ b/src/NUnitEngine/nunit.engine.core/Drivers/NUnitNetCore31Driver.cs @@ -38,6 +38,7 @@ public class NUnitNetCore31Driver : IFrameworkDriver Assembly _frameworkAssembly; object _frameworkController; Type _frameworkControllerType; + CustomAssemblyLoadContext _assemblyLoadContext; /// /// An id prefix that will be passed to the test framework and used as part of the @@ -56,10 +57,11 @@ public string Load(string assemblyPath, IDictionary settings) var idPrefix = string.IsNullOrEmpty(ID) ? "" : ID + "-"; assemblyPath = Path.GetFullPath(assemblyPath); //AssemblyLoadContext requires an absolute path - var assemblyLoadContext = new CustomAssemblyLoadContext(assemblyPath); + _assemblyLoadContext = new CustomAssemblyLoadContext(assemblyPath); + try { - _testAssembly = assemblyLoadContext.LoadFromAssemblyPath(assemblyPath); + _testAssembly = _assemblyLoadContext.LoadFromAssemblyPath(assemblyPath); } catch (Exception e) { @@ -67,11 +69,11 @@ public string Load(string assemblyPath, IDictionary settings) } var nunitRef = _testAssembly.GetReferencedAssemblies().FirstOrDefault(reference => reference.Name.Equals("nunit.framework", StringComparison.OrdinalIgnoreCase)); - + if (nunitRef == null) throw new NUnitEngineException(FAILED_TO_LOAD_NUNIT); - _frameworkAssembly = assemblyLoadContext.LoadFromAssemblyName(nunitRef); + _frameworkAssembly = _assemblyLoadContext.LoadFromAssemblyName(nunitRef); if (_frameworkAssembly == null) throw new NUnitEngineException(FAILED_TO_LOAD_NUNIT); @@ -112,7 +114,7 @@ public string Run(ITestEventListener listener, string filter) } /// - /// Executes the tests in an assembly asyncronously. + /// Executes the tests in an assembly asynchronously. /// /// A callback that receives XML progress notices /// A filter that controls which tests are executed @@ -179,7 +181,11 @@ object ExecuteMethod(MethodInfo method, params object[] args) { throw new NUnitEngineException(INVALID_FRAMEWORK_MESSAGE); } - return method.Invoke(_frameworkController, args); + + using (_assemblyLoadContext.EnterContextualReflection()) + { + return method.Invoke(_frameworkController, args); + } } } }