You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DevOps流水线发布时Newtonsoft.Json程序集版本错误问题问询

解决方案
  • 强制锁定Newtonsoft.Json版本
    在服务端csproj文件中显式声明高版本包引用:
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />

同时可在项目根目录的NuGet.config中添加全局版本锁定规则,强制还原时忽略其他传递依赖的版本要求:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
  <packageVersioning>
    <packageVersion id="Newtonsoft.Json" version="[13.0.1]" />
  </packageVersioning>
</configuration>

方括号包裹的版本号代表严格匹配,由于Microsoft.AspNetCore.Mvc.NewtonsoftJson的依赖要求为>=12.0,13.0.1版本完全兼容,不会出现兼容性问题。

  • 添加程序集绑定重定向
    .NET 5仍然支持传统的绑定重定向配置,在服务端项目新建/修改web.config文件,添加如下配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\你的服务端项目名.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.1.0" newVersion="13.0.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

该配置会强制运行时将所有对低版本Newtonsoft.Json的调用重定向到13.0.1版本。

  • 调整Azure流水线构建参数
  1. 在NuGet还原步骤添加--no-cache参数,避免流水线缓存的旧版本包干扰:
dotnet restore 你的解决方案名.sln --no-cache
  1. 发布步骤添加--force参数,强制使用指定版本依赖构建:
dotnet publish 服务端项目路径.csproj -c Release -o ./publish --force
  • 排查传递依赖冲突
    本地执行命令检查所有项目的传递依赖,确认是否存在其他第三方包引用了9.0.1版本的Newtonsoft.Json:
dotnet list package --include-transitive | findstr Newtonsoft.Json

如果存在其他包依赖低版本,可针对该包添加引用排除规则。

内容的提问来源于stack exchange,提问作者Joe Robe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 23:45:01