@@ -15,31 +15,57 @@ internal sealed class TestAssemblyLoadContext : AssemblyLoadContext
15
15
private readonly string _testAssemblyPath ;
16
16
private readonly string _basePath ;
17
17
private readonly TestAssemblyResolver _resolver ;
18
+ private readonly System . Runtime . Loader . AssemblyDependencyResolver _runtimeResolver ;
18
19
19
20
public TestAssemblyLoadContext ( string testAssemblyPath )
20
21
{
21
22
_testAssemblyPath = testAssemblyPath ;
22
23
_resolver = new TestAssemblyResolver ( this , testAssemblyPath ) ;
23
24
_basePath = Path . GetDirectoryName ( testAssemblyPath ) ;
25
+ _runtimeResolver = new AssemblyDependencyResolver ( testAssemblyPath ) ;
24
26
}
25
27
26
28
protected override Assembly Load ( AssemblyName name )
27
29
{
28
30
var assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
29
31
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
+ }
30
54
55
+ loadedAssembly = _resolver . Resolve ( this , name ) ;
31
56
if ( loadedAssembly != null )
32
- loadedAssembly = base . Load ( name ) ;
57
+ {
58
+ return loadedAssembly ;
59
+ }
33
60
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 ) )
35
67
{
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 ) ;
43
69
}
44
70
45
71
return loadedAssembly ;
0 commit comments