diff --git a/src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs b/src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs index f99a50fc26ce..d6a500d4c6ce 100644 --- a/src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs +++ b/src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs @@ -25,6 +25,12 @@ namespace Microsoft.Azure.Commands.Profile.Utilities public static class CustomAssemblyResolver { private static IDictionary NetFxPreloadAssemblies = ConditionalAssemblyProvider.GetAssemblies(); + private static ISet CrossMajorVersionRedirectionAllowList = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "System.Diagnostics.DiagnosticSource", + "System.Runtime.CompilerServices.Unsafe", + "Newtonsoft.Json" + }; public static void Initialize() { @@ -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); } @@ -56,5 +61,16 @@ public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEvent } return null; } + + /// + /// 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. + /// + /// + /// + private static bool IsCrossMajorVersionRedirectionAllowed(string assemblyName) + { + return CrossMajorVersionRedirectionAllowList.Contains(assemblyName); + } } }