dotnet/msbuild

Remove assumption that MSBuild controls AssemblyLoadContext.Default

rainersigwald opened this issue · 0 comments

MSBuild has several explicit references to AssemblyLoadContext.Default. That prevents an application from using the MSBuild API in a disposable (or otherwise managed) context.

return AssemblyLoadContext.Default.LoadFromAssemblyPath(_taskAssemblyFile);

if (FileSystems.Default.FileExists(assemblyNameInExecutableDirectory))
{
return AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyNameInExecutableDirectory);
}

This one is potentially OK since it's only to support an escape-hatch feature, but we can also consider removing it:

private Assembly LoadUsingLegacyDefaultContext(string fullPath)
{
lock (_guard)
{
if (!_resolvingHandlerHookedUp)
{
AssemblyLoadContext.Default.Resolving += TryResolveAssembly;
_resolvingHandlerHookedUp = true;
}
Assembly assembly;
if (_pathsToAssemblies.TryGetValue(fullPath, out assembly))
{
return assembly;
}
return LoadAndCache(AssemblyLoadContext.Default, fullPath);
}
}