我得到了错误

fileloadexception:无法加载文件或程序集 “Newtonsoft。Json,版本=4.5.0.0,文化=中性, PublicKeyToken=30ad4fe6b2a6aeed'或其依赖项之一。的 定位程序集的清单定义与该程序集不匹配 参考。(异常来自HRESULT: 0x80131040)

用于我的CI构建

我尝试过的解决方案

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
        culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

它也没有起作用


当前回答

右键单击您的项目选择管理Nuget包,在搜索框中输入newtonsoft并安装最新版本。然后运行你的应用

其他回答

另外,在使用NuGet还原的CI环境中,确保没有将部分文件夹检入源代码控制。默认情况下,在向源代码控制添加文件夹时,它将自动排除程序集。此外,这违背了在构建时恢复核包的整个目的。检查程序集或不检入任何包文件夹都将获得成功的构建,但更好的实践是不将包检入源代码控制。

在我的案例中,在下载程序集并将引用添加到项目后,我通过在将引用添加到项目之前“解锁”DLL来解决这个问题。

使用Windows资源管理器,浏览到DLL位置,右键单击DLL,然后选择“属性”。您将在其中一个选项卡上找到一个“unblock”按钮,然后您可以添加引用,程序集将正确加载。

我修复了添加这个绑定重定向到我的.config文件的问题:

<runtime>
    . . . 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
                culture="neutral" />
            <bindingRedirect oldVersion="4.5.0.0" newVersion="6.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

错误消息抱怨找不到版本4.5.0.0,当前版本的Newtonsoft。Json是6.0.0.0,所以重定向应该从4.5到6.0,而不是相反

对于一个最简单的解决方案,你可以设置你的项目文件自动生成绑定重定向:

<PropertyGroup>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection

另一个潜在的问题是,如果元素在任何其他dependentAssembly元素上有不正确的配置,那么绑定重定向似乎就会无声地失败。

确保每个元素下只有一个元素。

在某些情况下,VS生成这个:

  <dependentAssembly>
    <assemblyIdentity ...
    <assemblyIdentity ...
  </dependentAssembly>

而不是

  <dependentAssembly>
    <assemblyIdentity ...
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity ...
  </dependentAssembly>

我花了很长时间才意识到这就是问题所在!