Skip to content

Commit d2982c5

Browse files
authored
workaround the assembly loading issue (#22551)
1 parent f84cff8 commit d2982c5

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ namespace Microsoft.Azure.Commands.Profile.Utilities
2525
public static class CustomAssemblyResolver
2626
{
2727
private static IDictionary<string, (string Path, Version Version)> NetFxPreloadAssemblies = ConditionalAssemblyProvider.GetAssemblies();
28+
private static ISet<string> CrossMajorVersionRedirectionAllowList = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
29+
{
30+
"System.Diagnostics.DiagnosticSource",
31+
"System.Runtime.CompilerServices.Unsafe",
32+
"Newtonsoft.Json"
33+
};
2834

2935
public static void Initialize()
3036
{
@@ -42,10 +48,9 @@ public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEvent
4248
AssemblyName name = new AssemblyName(args.Name);
4349
if (NetFxPreloadAssemblies.TryGetValue(name.Name, out var assembly))
4450
{
45-
//For Newtonsoft.Json, allow to use bigger version to replace smaller version
4651
if (assembly.Version >= name.Version
4752
&& (assembly.Version.Major == name.Version.Major
48-
|| string.Equals(name.Name, "Newtonsoft.Json", StringComparison.OrdinalIgnoreCase)))
53+
|| IsCrossMajorVersionRedirectionAllowed(name.Name)))
4954
{
5055
return Assembly.LoadFrom(assembly.Path);
5156
}
@@ -56,5 +61,16 @@ public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEvent
5661
}
5762
return null;
5863
}
64+
65+
/// <summary>
66+
/// We allow cross major version redirection for some assemblies to avoid shipping multiple versions of the same assembly.
67+
/// Cautious should be taken when adding new assemblies to the allow list - make sure the new version is backward compatible.
68+
/// </summary>
69+
/// <param name="assemblyName"></param>
70+
/// <returns></returns>
71+
private static bool IsCrossMajorVersionRedirectionAllowed(string assemblyName)
72+
{
73+
return CrossMajorVersionRedirectionAllowList.Contains(assemblyName);
74+
}
5975
}
6076
}

0 commit comments

Comments
 (0)