Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ namespace Microsoft.Azure.Commands.Profile.Utilities
public static class CustomAssemblyResolver
{
private static IDictionary<string, (string Path, Version Version)> NetFxPreloadAssemblies = ConditionalAssemblyProvider.GetAssemblies();
private static ISet<string> CrossMajorVersionRedirectionAllowList = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"System.Diagnostics.DiagnosticSource",
"System.Runtime.CompilerServices.Unsafe",
"Newtonsoft.Json"
};

public static void Initialize()
{
Expand All @@ -42,10 +48,9 @@ public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEvent
AssemblyName name = new AssemblyName(args.Name);
if (NetFxPreloadAssemblies.TryGetValue(name.Name, out var assembly))
{
//For Newtonsoft.Json, allow to use bigger version to replace smaller version
if (assembly.Version >= name.Version
&& (assembly.Version.Major == name.Version.Major
|| string.Equals(name.Name, "Newtonsoft.Json", StringComparison.OrdinalIgnoreCase)))
|| IsCrossMajorVersionRedirectionAllowed(name.Name)))
{
return Assembly.LoadFrom(assembly.Path);
}
Expand All @@ -56,5 +61,16 @@ public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEvent
}
return null;
}

/// <summary>
/// We allow cross major version redirection for some assemblies to avoid shipping multiple versions of the same assembly.
/// Cautious should be taken when adding new assemblies to the allow list - make sure the new version is backward compatible.
/// </summary>
/// <param name="assemblyName"></param>
/// <returns></returns>
private static bool IsCrossMajorVersionRedirectionAllowed(string assemblyName)
{
return CrossMajorVersionRedirectionAllowList.Contains(assemblyName);
}
}
}