Skip to content

Commit b92df46

Browse files
authored
Merge pull request #1318 from svg2003/issue-1311-ver4
fix assembly dependencies resolver for netcore
2 parents c53d4f7 + 485153d commit b92df46

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyLoadContext.cs

+35-9
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,57 @@ internal sealed class TestAssemblyLoadContext : AssemblyLoadContext
1515
private readonly string _testAssemblyPath;
1616
private readonly string _basePath;
1717
private readonly TestAssemblyResolver _resolver;
18+
private readonly System.Runtime.Loader.AssemblyDependencyResolver _runtimeResolver;
1819

1920
public TestAssemblyLoadContext(string testAssemblyPath)
2021
{
2122
_testAssemblyPath = testAssemblyPath;
2223
_resolver = new TestAssemblyResolver(this, testAssemblyPath);
2324
_basePath = Path.GetDirectoryName(testAssemblyPath);
25+
_runtimeResolver = new AssemblyDependencyResolver(testAssemblyPath);
2426
}
2527

2628
protected override Assembly Load(AssemblyName name)
2729
{
2830
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
2931
var loadedAssembly = assemblies.FirstOrDefault(x => x.GetName().Name == name.Name);
32+
if (loadedAssembly != null)
33+
{
34+
return loadedAssembly;
35+
}
36+
37+
loadedAssembly = base.Load(name);
38+
if (loadedAssembly != null)
39+
{
40+
return loadedAssembly;
41+
}
42+
43+
var runtimeResolverPath = _runtimeResolver.ResolveAssemblyToPath(name);
44+
if (string.IsNullOrEmpty(runtimeResolverPath) == false &&
45+
File.Exists(runtimeResolverPath))
46+
{
47+
loadedAssembly = LoadFromAssemblyPath(runtimeResolverPath);
48+
}
49+
50+
if (loadedAssembly != null)
51+
{
52+
return loadedAssembly;
53+
}
3054

55+
loadedAssembly = _resolver.Resolve(this, name);
3156
if (loadedAssembly != null)
32-
loadedAssembly = base.Load(name);
57+
{
58+
return loadedAssembly;
59+
}
3360

34-
if (loadedAssembly == null)
61+
// Load assemblies that are dependencies, and in the same folder as the test assembly,
62+
// but are not fully specified in test assembly deps.json file. This happens when the
63+
// dependencies reference in the csproj file has CopyLocal=false, and for example, the
64+
// reference is a projectReference and has the same output directory as the parent.
65+
string assemblyPath = Path.Combine(_basePath, name.Name + ".dll");
66+
if (File.Exists(assemblyPath))
3567
{
36-
// Load assemblies that are dependencies, and in the same folder as the test assembly,
37-
// but are not fully specified in test assembly deps.json file. This happens when the
38-
// dependencies reference in the csproj file has CopyLocal=false, and for example, the
39-
// reference is a projectReference and has the same output directory as the parent.
40-
string assemblyPath = Path.Combine(_basePath, name.Name + ".dll");
41-
if (File.Exists(assemblyPath))
42-
loadedAssembly = LoadFromAssemblyPath(assemblyPath);
68+
loadedAssembly = LoadFromAssemblyPath(assemblyPath);
4369
}
4470

4571
return loadedAssembly;

src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyResolver.cs

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public void Dispose()
5555
_loadContext.Resolving -= OnResolving;
5656
}
5757

58+
public Assembly Resolve(AssemblyLoadContext context, AssemblyName name)
59+
{
60+
return OnResolving(context, name);
61+
}
62+
5863
private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
5964
{
6065
foreach (var library in _dependencyContext.RuntimeLibraries)

0 commit comments

Comments
 (0)