Skip to content

Commit

Permalink
Merge pull request #1318 from svg2003/issue-1311-ver4
Browse files Browse the repository at this point in the history
fix assembly dependencies resolver for netcore
  • Loading branch information
rprouse authored Feb 23, 2023
2 parents c53d4f7 + 485153d commit b92df46
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,57 @@ internal sealed class TestAssemblyLoadContext : AssemblyLoadContext
private readonly string _testAssemblyPath;
private readonly string _basePath;
private readonly TestAssemblyResolver _resolver;
private readonly System.Runtime.Loader.AssemblyDependencyResolver _runtimeResolver;

public TestAssemblyLoadContext(string testAssemblyPath)
{
_testAssemblyPath = testAssemblyPath;
_resolver = new TestAssemblyResolver(this, testAssemblyPath);
_basePath = Path.GetDirectoryName(testAssemblyPath);
_runtimeResolver = new AssemblyDependencyResolver(testAssemblyPath);
}

protected override Assembly Load(AssemblyName name)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var loadedAssembly = assemblies.FirstOrDefault(x => x.GetName().Name == name.Name);
if (loadedAssembly != null)
{
return loadedAssembly;
}

loadedAssembly = base.Load(name);
if (loadedAssembly != null)
{
return loadedAssembly;
}

var runtimeResolverPath = _runtimeResolver.ResolveAssemblyToPath(name);
if (string.IsNullOrEmpty(runtimeResolverPath) == false &&
File.Exists(runtimeResolverPath))
{
loadedAssembly = LoadFromAssemblyPath(runtimeResolverPath);
}

if (loadedAssembly != null)
{
return loadedAssembly;
}

loadedAssembly = _resolver.Resolve(this, name);
if (loadedAssembly != null)
loadedAssembly = base.Load(name);
{
return loadedAssembly;
}

if (loadedAssembly == null)
// Load assemblies that are dependencies, and in the same folder as the test assembly,
// but are not fully specified in test assembly deps.json file. This happens when the
// dependencies reference in the csproj file has CopyLocal=false, and for example, the
// reference is a projectReference and has the same output directory as the parent.
string assemblyPath = Path.Combine(_basePath, name.Name + ".dll");
if (File.Exists(assemblyPath))
{
// Load assemblies that are dependencies, and in the same folder as the test assembly,
// but are not fully specified in test assembly deps.json file. This happens when the
// dependencies reference in the csproj file has CopyLocal=false, and for example, the
// reference is a projectReference and has the same output directory as the parent.
string assemblyPath = Path.Combine(_basePath, name.Name + ".dll");
if (File.Exists(assemblyPath))
loadedAssembly = LoadFromAssemblyPath(assemblyPath);
loadedAssembly = LoadFromAssemblyPath(assemblyPath);
}

return loadedAssembly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public void Dispose()
_loadContext.Resolving -= OnResolving;
}

public Assembly Resolve(AssemblyLoadContext context, AssemblyName name)
{
return OnResolving(context, name);
}

private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
{
foreach (var library in _dependencyContext.RuntimeLibraries)
Expand Down

0 comments on commit b92df46

Please sign in to comment.