Skip to content

Commit

Permalink
Use new EnterContextualReflection API to address .NET Core assembly l…
Browse files Browse the repository at this point in the history
…oading issues
  • Loading branch information
ChrisMaddock committed May 2, 2021
1 parent 154866e commit a4502fb
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/NUnitEngine/nunit.engine.core/Drivers/NUnitNetCore31Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class NUnitNetCore31Driver : IFrameworkDriver
Assembly _frameworkAssembly;
object _frameworkController;
Type _frameworkControllerType;
CustomAssemblyLoadContext _assemblyLoadContext;

/// <summary>
/// An id prefix that will be passed to the test framework and used as part of the
Expand All @@ -56,22 +57,23 @@ public string Load(string assemblyPath, IDictionary<string, object> 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)
{
throw new NUnitEngineException(string.Format(FAILED_TO_LOAD_TEST_ASSEMBLY, assemblyPath), e);
}

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

Expand Down Expand Up @@ -112,7 +114,7 @@ public string Run(ITestEventListener listener, string filter)
}

/// <summary>
/// Executes the tests in an assembly asyncronously.
/// Executes the tests in an assembly asynchronously.
/// </summary>
/// <param name="callback">A callback that receives XML progress notices</param>
/// <param name="filter">A filter that controls which tests are executed</param>
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down

0 comments on commit a4502fb

Please sign in to comment.