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

时间:2018-04-18 17:32:11

标签: c# model-view-controller dll json.net assembly-binding-redirect

我开发了一个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 resp下。 - 项目参考中引用的版本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>     

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

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

谢谢, GAGAN

3 个答案:

答案 0 :(得分:4)

请尝试使用此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\7.0.0.0\Newtonsoft.Json.dll" />
    </dependentAssembly>
  </assemblyBinding>

答案 1 :(得分:1)

I had also hit the same issue. I solved using codebase methods mentioned in this link. https://devnet.kentico.com/articles/referencing-multiple-versions-of-the-same-assembly-in-a-single-application

答案 2 :(得分:0)

这是我的两分钱。
这是我用于版本升级以及重定向到旧版本的方法:

<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>