3

I have a DLL that uses assembly binding via it's .config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

This is needed because the dependencies load conflicting versions of Newtonsoft.Json (6.0 vs. 9.0) and the binding is needed to resolve the conflict.

the problem I have is that this DLL is a Powershell module, and is loaded by powershell. Its .config is ignored and the runtime throws exception:

My-cmdLet : Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral,
ublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

Normally I would resolve this by modifying the hosting exe .config file and adding the relevant dll section. But in this case the hosting .exe is Powershell, and I'd rather not mess with that .config.

Is there a way the DLL itself can resolve the binding properly?

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • 1
    You can subscribe to `AppDomain.CurrentDomain.AssemblyResolve` event and handle redirection there (either from powershell itself, or from your dll, though doing that from dll might be tricky). – Evk Nov 27 '17 at 13:57
  • Put it in the GAC, resistance is futile. Or solve the problem the programmer's way: decompile the assembly with ildasm.exe, change the .assembly directive, put back together with ildasm.exe – Hans Passant Nov 27 '17 at 15:35
  • @HansPassant GAC imposes deployment burden (is no longer an xcopy deploy), and assembly editing imposes build burden (I must push a modified nuget package for Microsoft.Rest...). I tried Evk solution and seems to be functional. Does it cause any unforeseen problems? – Remus Rusanu Nov 28 '17 at 07:09

0 Answers0