diff --git a/src/Accounts/Accounts/StartupScripts/InitializeAssemblyResolver.ps1 b/src/Accounts/Accounts/StartupScripts/InitializeAssemblyResolver.ps1 index 91c32b461788..0b7de449c2fa 100644 --- a/src/Accounts/Accounts/StartupScripts/InitializeAssemblyResolver.ps1 +++ b/src/Accounts/Accounts/StartupScripts/InitializeAssemblyResolver.ps1 @@ -1,5 +1,7 @@ if ($PSEdition -eq 'Desktop') { try { [Microsoft.Azure.Commands.Profile.Utilities.CustomAssemblyResolver]::Initialize() - } catch {} + } catch { + Write-Warning $_ + } } \ No newline at end of file diff --git a/src/Accounts/Accounts/Utilities/CustomAssemblyResolver.cs b/src/Accounts/Accounts/Utilities/CustomAssemblyResolver.cs deleted file mode 100644 index b45e206befe0..000000000000 --- a/src/Accounts/Accounts/Utilities/CustomAssemblyResolver.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Reflection; -using Newtonsoft.Json; - -namespace Microsoft.Azure.Commands.Profile.Utilities -{ - public static class CustomAssemblyResolver - { - private static IDictionary NetFxPreloadAssemblies = - new Dictionary(StringComparer.InvariantCultureIgnoreCase); - - private static string PreloadAssemblyFolder { get; set; } - - public static void Initialize() - { - var accountFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - PreloadAssemblyFolder = Path.Combine(accountFolder, "PreloadAssemblies"); - var assemblyInfo = File.ReadAllText(Path.Combine(accountFolder, "PreloadAssemblyInfo.json")); - - var root = JsonConvert.DeserializeObject>>(assemblyInfo); - var netfx = root["netfx"]; - foreach(var info in netfx) - { - NetFxPreloadAssemblies[info.Key] = new Version(info.Value); - } - - AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; - } - - /// - /// When the resolution of an assembly fails, if will try to redirect to the higher version - /// - public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) - { - try - { - AssemblyName name = new AssemblyName(args.Name); - if (NetFxPreloadAssemblies.TryGetValue(name.Name, out Version version)) - { - if (version >= name.Version && version.Major == name.Version.Major) - { - string requiredAssembly = Path.Combine(PreloadAssemblyFolder, $"{name.Name}.dll"); - return Assembly.LoadFrom(requiredAssembly); - } - } - } - catch - { - } - return null; - } - } -} diff --git a/src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs b/src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs new file mode 100644 index 000000000000..6d83992d1b9d --- /dev/null +++ b/src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; + +namespace Microsoft.Azure.Commands.Profile.Utilities +{ + public static class CustomAssemblyResolver + { + private static IDictionary NetFxPreloadAssemblies = + new Dictionary(StringComparer.InvariantCultureIgnoreCase) + { + {"Azure.Core", new Version("1.2.1.0")}, + {"Microsoft.Bcl.AsyncInterfaces", new Version("1.0.0.0")}, + {"Microsoft.IdentityModel.Clients.ActiveDirectory", new Version("3.19.2.6005")}, + {"Microsoft.IdentityModel.Clients.ActiveDirectory.Platform", new Version("3.19.2.6005")}, + {"Newtonsoft.Json", new Version("10.0.0.0")}, + {"System.Buffers", new Version("4.0.2.0")}, + {"System.Diagnostics.DiagnosticSource", new Version("4.0.4.0")}, + {"System.Memory", new Version("4.0.1.1")}, + {"System.Net.Http.WinHttpHandler", new Version("4.0.2.0")}, + {"System.Numerics.Vectors", new Version("4.1.3.0")}, + {"System.Private.ServiceModel", new Version("4.1.2.1")}, + {"System.Reflection.DispatchProxy", new Version("4.0.3.0")}, + {"System.Runtime.CompilerServices.Unsafe", new Version("4.0.5.0")}, + {"System.Security.AccessControl", new Version("4.1.1.0")}, + {"System.Security.Permissions", new Version("4.0.1.0")}, + {"System.Security.Principal.Windows", new Version("4.1.1.0")}, + {"System.ServiceModel.Primitives", new Version("4.2.0.0")}, + {"System.Text.Encodings.Web", new Version("4.0.4.0")}, + {"System.Text.Json", new Version("4.0.0.0")}, + {"System.Threading.Tasks.Extensions", new Version("4.2.0.0")}, + {"System.Xml.ReaderWriter", new Version("4.1.0.0")} + }; + + private static string PreloadAssemblyFolder { get; set; } + + public static void Initialize() + { + //This function is call before loading assemblies in PreloadAssemblies folder, so NewtonSoft.Json could not be used here + var accountFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + PreloadAssemblyFolder = Path.Combine(accountFolder, "PreloadAssemblies"); + AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; + } + + /// + /// When the resolution of an assembly fails, if will try to redirect to the higher version + /// + public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + try + { + AssemblyName name = new AssemblyName(args.Name); + if (NetFxPreloadAssemblies.TryGetValue(name.Name, out Version version)) + { + if (version >= name.Version && version.Major == name.Version.Major) + { + string requiredAssembly = Path.Combine(PreloadAssemblyFolder, $"{name.Name}.dll"); + return Assembly.LoadFrom(requiredAssembly); + } + } + } + catch + { + } + return null; + } + } +}