ASP.NET MVC 需要两个不同版本的 Newtonsoft.Json.dll

Two different versions of Newtonsoft.Json.dll needed in ASP.NET MVC

我开发了一个 MVC 应用程序,它依赖于使用 Newtonsoft.Json.dll v6.0.0.0 的 Connectwise SDK 和使用 Newtonsoft.Json.dll v7.0.0.0.

的 Dropbox SDK

我需要确保我的项目在需要时使用适当的 dll。 经过研究,我尝试了以下方法: - 将 2 个 dll 分别放在子文件夹 /dlls/6.0.0.0/ 和 /dlls/7.0.0.0 下。 - 项目参考中引用的版本 6.0.0.0 dll - 添加到 web.config

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
            culture="neutral" />
          <bindingredirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"></bindingredirect>          
        </dependentAssembly>
       <dependentAssembly>
          <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
            culture="neutral" />
          <bindingredirect oldVersion="7.0.0.0-7.1.0.0" newVersion="7.0.0.0"></bindingredirect>
          <codeBase version="7.0.0.0" href="dlls/7.0.0.0/Newtonsoft.Json.dll" />
        </dependentAssembly>
      </assemblyBinding>     

无法加载文件或程序集 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (HRESULT 异常:0x80131040)

我的 href 不正确吗?? dlls 文件夹与 MVC 项目中的 Content 文件夹处于同一级别

谢谢, 加根

我也遇到了同样的问题。我使用此 link 中提到的代码库方法解决了问题。 https://devnet.kentico.com/articles/referencing-multiple-versions-of-the-same-assembly-in-a-single-application

试试这个 assemblyBinding 块,注意细微差别...

我已经删除了 bindingredirect 节点,因为你不需要它!

我已将您的 href 属性中的斜杠更改为反斜杠

当然你需要 2 个 codeBase 节点

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
   <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
        culture="neutral" />
      <!-- You don't need binding redirect, because you're not targeting old version to new,
      ---- instead you're catering to 2 different versions.
        <bindingredirect oldVersion="7.0.0.0-7.1.0.0" newVersion="7.0.0.0"></bindingredirect>
      -->
      <codeBase version="6.0.0.0" href="bin\json6\Newtonsoft.Json.dll" />
      <codeBase version="7.0.0.0" href="dlls.0.0.0\Newtonsoft.Json.dll" />
    </dependentAssembly>
  </assemblyBinding>

这是我的两分钱。
这是我用于版本升级和重定向到旧版本的内容:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.5.0.0" />   
    <codeBase version="4.5.0.0" href="bin/OldJson/Newtonsoft.Json.dll" />
  </dependentAssembly>
</assemblyBinding>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="5.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
  </dependentAssembly>
</assemblyBinding>