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

升级.NET 7后dotnet publish --no-build命令失效问题排查

.NET 7 SDK安装后Cake脚本发布报错问题分析与解决

问题背景

我们的CI/CD流水线基于Cake脚本实现,此前运行多年无异常。但在安装Visual Studio 17.4(附带.NET 7 SDK及MSBuild 17.4)后,发布步骤出现异常。Cake生成的相关dotnet命令如下:

  • 还原NuGet包:"C:/Program Files/dotnet/dotnet.exe" restore "Infrastructure.LogAggregator.sln"
  • 构建应用:"C:/Program Files/dotnet/dotnet.exe" build "Infrastructure.LogAggregator.sln" --configuration Release --no-restore
  • 发布已构建的应用:"C:/Program Files/dotnet/dotnet.exe" publish "Infrastructure.LogAggregator/Infrastructure.LogAggregator.csproj" --output "C:/Users/uapwa/AppData/Local/Temp/Infrastructure.LogAggregator-75cea93a-9955-4907-925e-89c2fac4fc03" --framework net6.0 --configuration Release --no-build --self-contained true

报错详情

发布步骤抛出以下错误:

C:\Program Files\dotnet\sdk\7.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1047: Assets file 'D:\Dev\Infrastructure.LogAggregator\Source\Infrastructure.LogAggregator\obj\project.assets.json' doesn't have a target for 'net6.0/win-x64'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project. You may also need to include 'win-x64' in your project's RuntimeIdentifiers. [D:\Dev\Infrastructure.LogAggregator\Source\Infrastructure.LogAggregator\Infrastructure.LogAggregator.csproj]

尝试在构建命令中添加--runtime win-x64参数时,又出现新错误:

C:\Program Files\dotnet\sdk\7.0.100\Current\SolutionFile\ImportAfter\Microsoft.NET.Sdk.Solution.targets(27,5): error NETSDK1134: Building a solution with a specific RuntimeIdentifier is not supported. If you would like to publish for a single RID, specifiy the RID at the individual project level instead. [D:\Dev\Infrastructure.LogAggregator\Source\Infrastructure.LogAggregator.sln]

原因解析

.NET 7 SDK对自包含发布的依赖解析逻辑做了严格调整:

  • 自包含发布需要将目标运行时的依赖包一起打包,这要求project.assets.json中必须包含对应RuntimeIdentifier(RID)的目标信息。
  • 此前的流程中,restore和build都是针对整个解决方案执行的,没有绑定到具体RID,生成的资产文件仅包含与框架无关的依赖,缺少自包含发布所需的特定RID依赖条目。
  • .NET 6及更早版本的SDK可能默认处理了RID的隐式生成,或者依赖解析逻辑更宽松,但.NET 7明确要求RID上下文必须在restore或构建阶段明确指定。

解决方法

方法1:在项目文件中指定RuntimeIdentifier

直接在项目的.csproj文件中添加RuntimeIdentifier属性,明确目标运行时:

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>net6.0</TargetFramework>
  <Version>0.0.0.1</Version>
  <AssemblyVersion>0.0.0.1</AssemblyVersion>
  <FileVersion>0.0.0.1</FileVersion>
  <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  <ImplicitUsings>enable</ImplicitUsings>
  <Nullable>enable</Nullable>
</PropertyGroup>

这样在restore和build时,会自动生成对应RID的资产信息,满足自包含发布的要求。

方法2:发布时移除--no-build参数

修改发布命令,去掉--no-build,让发布过程自动完成构建(会自动处理RID相关的资产生成):
对应的dotnet命令:

"C:/Program Files/dotnet/dotnet.exe" publish "Infrastructure.LogAggregator/Infrastructure.LogAggregator.csproj" --output "C:/Users/uapwa/AppData/Local/Temp/Infrastructure.LogAggregator-145e3f13-8442-4e7b-9ebc-ed0cee7aafd3" --framework net6.0 --configuration Release --self-contained true

对应的Cake脚本修改:移除DotNetPublishSettings中的NoBuild = true配置:

var publishSettings = new DotNetPublishSettings
                      {
                          Framework = "net6.0",
                          Configuration = _configuration,
                          OutputDirectory = deployPath,
                          SelfContained = true
                          // 移除NoBuild = true
                      };

为什么现在需要这样操作?

.NET 7 SDK在依赖解析和资产生成环节做了优化,对自包含发布的RID上下文要求更明确:

  • 自包含发布本质是针对特定运行环境打包,必须依赖对应RID的资产信息,而之前的跨RID资产生成逻辑被调整为更严格的显式指定模式。
  • 解决方案级别的build无法绑定RID(这也是NETSDK1134错误的原因),因此要么在项目层面预先指定RID,要么让发布过程自行处理构建(此时会基于项目的目标框架和自包含要求自动推导RID并生成对应资产)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 06:15:42