Switch from AssemblyLoadContext.InitializeDefaultContext to AssemblyLoadContext.Resolving event

AssemblyLoadContext.InitializeDefaultContext was removed for .NET Core 1.0 RTM. Replaced its use
with AssemblyLoadContext.Resolving event.
This commit is contained in:
Jan Kotas 2016-05-20 14:14:30 -07:00 committed by Andrew Schwartzmeyer
parent a02e89ee7b
commit 44ba0bb28f
2 changed files with 33 additions and 39 deletions

View file

@ -15,9 +15,9 @@ using System.Runtime.Loader;
namespace System.Management.Automation namespace System.Management.Automation
{ {
/// <summary> /// <summary>
/// The powershell custom AssemblyLoadContext implementation /// The powershell custom assembly loader implementation
/// </summary> /// </summary>
internal partial class PowerShellAssemblyLoadContext : AssemblyLoadContext internal partial class PowerShellAssemblyLoadContext
{ {
#region Resource_Strings #region Resource_Strings
@ -37,13 +37,6 @@ namespace System.Management.Automation
#region Constructor #region Constructor
/// <summary>
/// This constructor is for testability purpose only
/// </summary>
protected PowerShellAssemblyLoadContext()
{
}
/// <summary> /// <summary>
/// Constructor /// Constructor
/// </summary> /// </summary>
@ -77,12 +70,18 @@ namespace System.Management.Automation
// - Key: namespace qualified type name (FullName) // - Key: namespace qualified type name (FullName)
// - Value: strong name of the TPA that contains the type represented by Key. // - Value: strong name of the TPA that contains the type represented by Key.
coreClrTypeCatalog = InitializeTypeCatalog(); coreClrTypeCatalog = InitializeTypeCatalog();
this.loadContext = AssemblyLoadContext.Default;
loadContext.Resolving += Resolve;
} }
#endregion Constructor #endregion Constructor
#region Fields #region Fields
// AssemblyLoadContext used by this loader
private readonly AssemblyLoadContext loadContext;
// Serialized type catalog file // Serialized type catalog file
private readonly object syncObj = new object(); private readonly object syncObj = new object();
private readonly string[] basePaths; private readonly string[] basePaths;
@ -118,10 +117,15 @@ namespace System.Management.Automation
#region Protected_Internal_Methods #region Protected_Internal_Methods
/// <summary> /// <summary>
/// Implement the AssemblyLoadContext.Load(AssemblyName). Search the requested assembly in probing paths. /// The global instance of PowerShellAssemblyLoadContext
/// </summary>
internal static PowerShellAssemblyLoadContext Instance { get; set; }
/// <summary>
/// Implement the AssemblyLoadContext.Resolving event handler. Search the requested assembly in probing paths.
/// Search the file "[assemblyName.Name][.ni].dll" in probing paths. If the file is found and it matches the requested AssemblyName, load it with LoadFromAssemblyPath. /// Search the file "[assemblyName.Name][.ni].dll" in probing paths. If the file is found and it matches the requested AssemblyName, load it with LoadFromAssemblyPath.
/// </summary> /// </summary>
protected override Assembly Load(AssemblyName assemblyName) internal Assembly Resolve(AssemblyLoadContext sender, AssemblyName assemblyName)
{ {
// Probe the assembly cache // Probe the assembly cache
Assembly asmLoaded; Assembly asmLoaded;
@ -153,7 +157,7 @@ namespace System.Management.Automation
if (File.Exists(asmFilePath)) if (File.Exists(asmFilePath))
{ {
isAssemblyFileFound = true; isAssemblyFileFound = true;
AssemblyName asmNameFound = GetAssemblyName(asmFilePath); AssemblyName asmNameFound = AssemblyLoadContext.GetAssemblyName(asmFilePath);
if (IsAssemblyMatching(assemblyName, asmNameFound)) if (IsAssemblyMatching(assemblyName, asmNameFound))
{ {
isAssemblyFileMatching = true; isAssemblyFileMatching = true;
@ -187,8 +191,8 @@ namespace System.Management.Automation
try try
{ {
asmLoaded = asmFilePath.EndsWith(".ni.dll", StringComparison.OrdinalIgnoreCase) asmLoaded = asmFilePath.EndsWith(".ni.dll", StringComparison.OrdinalIgnoreCase)
? LoadFromNativeImagePath(asmFilePath, null) ? loadContext.LoadFromNativeImagePath(asmFilePath, null)
: LoadFromAssemblyPath(asmFilePath); : loadContext.LoadFromAssemblyPath(asmFilePath);
} }
// Since .NET CLI built versions of PowerShell have all the // Since .NET CLI built versions of PowerShell have all the
// built-in assemblies in the TPA list, the above will throw, // built-in assemblies in the TPA list, the above will throw,
@ -211,6 +215,14 @@ namespace System.Management.Automation
return asmLoaded; return asmLoaded;
} }
/// <summary>
/// Load an assembly from its name.
/// </summary>
internal Assembly LoadFromAssemblyName(AssemblyName assemblyName)
{
return loadContext.LoadFromAssemblyName(assemblyName);
}
/// <summary> /// <summary>
/// Load an assembly from its file path. /// Load an assembly from its file path.
/// </summary> /// </summary>
@ -239,7 +251,7 @@ namespace System.Management.Automation
#endregion Validation #endregion Validation
Assembly asmLoaded; Assembly asmLoaded;
AssemblyName assemblyName = GetAssemblyName(assemblyPath); AssemblyName assemblyName = AssemblyLoadContext.GetAssemblyName(assemblyPath);
// Probe the assembly cache // Probe the assembly cache
if (TryGetAssemblyFromCache(assemblyName, out asmLoaded)) if (TryGetAssemblyFromCache(assemblyName, out asmLoaded))
@ -256,8 +268,8 @@ namespace System.Management.Automation
{ {
// Load the assembly through 'LoadFromNativeImagePath' or 'LoadFromAssemblyPath' // Load the assembly through 'LoadFromNativeImagePath' or 'LoadFromAssemblyPath'
asmLoaded = assemblyPath.EndsWith(".ni.dll", StringComparison.OrdinalIgnoreCase) asmLoaded = assemblyPath.EndsWith(".ni.dll", StringComparison.OrdinalIgnoreCase)
? LoadFromNativeImagePath(assemblyPath, null) ? loadContext.LoadFromNativeImagePath(assemblyPath, null)
: LoadFromAssemblyPath(assemblyPath); : loadContext.LoadFromAssemblyPath(assemblyPath);
} }
// Since .NET CLI built versions of PowerShell have all the // Since .NET CLI built versions of PowerShell have all the
// built-in assemblies in the TPA list, the above will throw, // built-in assemblies in the TPA list, the above will throw,
@ -290,7 +302,7 @@ namespace System.Management.Automation
/// </summary> /// </summary>
internal Assembly LoadFrom(Stream assembly) internal Assembly LoadFrom(Stream assembly)
{ {
var asm = LoadFromStream(assembly); var asm = loadContext.LoadFromStream(assembly);
TryAddAssemblyToCache(asm); TryAddAssemblyToCache(asm);
return asm; return asm;
} }
@ -503,8 +515,6 @@ namespace System.Management.Automation
/// </summary> /// </summary>
public class PowerShellAssemblyLoadContextInitializer public class PowerShellAssemblyLoadContextInitializer
{ {
private static bool IsInitialized = false;
// Porting note: it's much easier to send an LPStr on Linux // Porting note: it's much easier to send an LPStr on Linux
private const UnmanagedType stringType = private const UnmanagedType stringType =
#if LINUX #if LINUX
@ -519,19 +529,9 @@ namespace System.Management.Automation
/// </summary> /// </summary>
public static void SetPowerShellAssemblyLoadContext([MarshalAs(stringType)]string basePaths) public static void SetPowerShellAssemblyLoadContext([MarshalAs(stringType)]string basePaths)
{ {
if (!IsInitialized) if (PowerShellAssemblyLoadContext.Instance == null)
{ {
var psAsmLoadContext = new PowerShellAssemblyLoadContext(basePaths); PowerShellAssemblyLoadContext.Instance = new PowerShellAssemblyLoadContext(basePaths);
try
{
AssemblyLoadContext.InitializeDefaultContext(psAsmLoadContext);
}
catch (System.InvalidOperationException)
{
// We may not be able to set the default context. If we're under the
// xUnit test harness, it has already been set.
}
IsInitialized = true;
} }
} }
} }

View file

@ -391,13 +391,7 @@ namespace System.Management.Automation
{ {
if (_psLoadContext == null) if (_psLoadContext == null)
{ {
_psLoadContext = AssemblyLoadContext.Default as PowerShellAssemblyLoadContext; _psLoadContext = PowerShellAssemblyLoadContext.Instance;
if (_psLoadContext == null)
{
// The default load context may not be ours. This can happen during,
// for instance, xUnit testing.
_psLoadContext = new PowerShellAssemblyLoadContext(String.Empty);
}
} }
return _psLoadContext; return _psLoadContext;
} }