Having collected this problem myself when developing a PowerShell module that uses several third-party libraries (Google API, Dropbox, Graph, etc.), I found the following solution the simplest:
public static Assembly CurrentDomain_BindingRedirect(object sender, ResolveEventArgs args)
{
var name = new AssemblyName(args.Name);
switch (name.Name)
{
case "Microsoft.Graph.Core":
return typeof(Microsoft.Graph.IBaseClient).Assembly;
case "Newtonsoft.Json":
return typeof(Newtonsoft.Json.JsonSerializer).Assembly;
case "System.Net.Http.Primitives":
return Assembly.LoadFrom("System.Net.Http.Primitives.dll");
default:
return null;
}
}
Please note that in the method I have two possible ways to reference the assembly, but both of them do the same thing, they force the current version of this assembly to be used. (Regardless of whether it is loaded through a class link or a dll file upload)
To use this in any cmd-let, add the following event handler to the BeginProcessing () method for PSCmdLet.
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_BindingRedirect;
source
share