How to redirect an assembly binding for a DLL irrelevant of the loading host

↘锁芯ラ 提交于 2019-12-08 03:23:42

问题


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?

来源:https://stackoverflow.com/questions/47512496/how-to-redirect-an-assembly-binding-for-a-dll-irrelevant-of-the-loading-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!