Skip to content

Commit 3f61839

Browse files
committed
Fix bug that StartupScripts call function that not loaded
1 parent 62bc769 commit 3f61839

File tree

3 files changed

+72
-56
lines changed

3 files changed

+72
-56
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
if ($PSEdition -eq 'Desktop') {
22
try {
33
[Microsoft.Azure.Commands.Profile.Utilities.CustomAssemblyResolver]::Initialize()
4-
} catch {}
4+
} catch {
5+
Write-Warning $_
6+
}
57
}

src/Accounts/Accounts/Utilities/CustomAssemblyResolver.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Reflection;
5+
6+
namespace Microsoft.Azure.Commands.Profile.Utilities
7+
{
8+
public static class CustomAssemblyResolver
9+
{
10+
private static IDictionary<string, Version> NetFxPreloadAssemblies =
11+
new Dictionary<string, Version>(StringComparer.InvariantCultureIgnoreCase)
12+
{
13+
{"Azure.Core", new Version("1.2.1.0")},
14+
{"Microsoft.Bcl.AsyncInterfaces", new Version("1.0.0.0")},
15+
{"Microsoft.IdentityModel.Clients.ActiveDirectory", new Version("3.19.2.6005")},
16+
{"Microsoft.IdentityModel.Clients.ActiveDirectory.Platform", new Version("3.19.2.6005")},
17+
{"Newtonsoft.Json", new Version("10.0.0.0")},
18+
{"System.Buffers", new Version("4.0.2.0")},
19+
{"System.Diagnostics.DiagnosticSource", new Version("4.0.4.0")},
20+
{"System.Memory", new Version("4.0.1.1")},
21+
{"System.Net.Http.WinHttpHandler", new Version("4.0.2.0")},
22+
{"System.Numerics.Vectors", new Version("4.1.3.0")},
23+
{"System.Private.ServiceModel", new Version("4.1.2.1")},
24+
{"System.Reflection.DispatchProxy", new Version("4.0.3.0")},
25+
{"System.Runtime.CompilerServices.Unsafe", new Version("4.0.5.0")},
26+
{"System.Security.AccessControl", new Version("4.1.1.0")},
27+
{"System.Security.Permissions", new Version("4.0.1.0")},
28+
{"System.Security.Principal.Windows", new Version("4.1.1.0")},
29+
{"System.ServiceModel.Primitives", new Version("4.2.0.0")},
30+
{"System.Text.Encodings.Web", new Version("4.0.4.0")},
31+
{"System.Text.Json", new Version("4.0.0.0")},
32+
{"System.Threading.Tasks.Extensions", new Version("4.2.0.0")},
33+
{"System.Xml.ReaderWriter", new Version("4.1.0.0")}
34+
};
35+
36+
private static string PreloadAssemblyFolder { get; set; }
37+
38+
public static void Initialize()
39+
{
40+
//This function is call before loading assemblies in PreloadAssemblies folder, so NewtonSoft.Json could not be used here
41+
var accountFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
42+
PreloadAssemblyFolder = Path.Combine(accountFolder, "PreloadAssemblies");
43+
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
44+
}
45+
46+
/// <summary>
47+
/// When the resolution of an assembly fails, if will try to redirect to the higher version
48+
/// </summary>
49+
public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
50+
{
51+
try
52+
{
53+
AssemblyName name = new AssemblyName(args.Name);
54+
if (NetFxPreloadAssemblies.TryGetValue(name.Name, out Version version))
55+
{
56+
if (version >= name.Version && version.Major == name.Version.Major)
57+
{
58+
string requiredAssembly = Path.Combine(PreloadAssemblyFolder, $"{name.Name}.dll");
59+
return Assembly.LoadFrom(requiredAssembly);
60+
}
61+
}
62+
}
63+
catch
64+
{
65+
}
66+
return null;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)