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

在ADO Pipeline构建.NET Core 6.0时遇MSB3971错误求助

修复Azure DevOps Pipeline中.NET 6项目MSBuild构建错误

你遇到的错误是因为在Ubuntu代理上,MSBuild@1任务默认调用的是Mono版本的MSBuild,它无法识别.NET 6的目标框架引用集。你的项目是.NET 6 SDK风格项目,需要适配正确的构建工具,以下是两种可行的修复方案:

方案一:改用DotNetCoreCLI任务构建(推荐)

.NET 5+项目更适合用官方的DotNetCoreCLI@2任务,它会自动使用已安装的.NET SDK进行构建,兼容性更好。将原YAML中的MSBuild@1步骤替换为以下内容:

- task: DotNetCoreCLI@2
  displayName: 'Build Test Automation Solution'
  inputs:
    command: 'build'
    projects: '$(solution)'
    arguments: '--configuration $(buildConfiguration) --output $(Build.BinariesDirectory)'

同时建议调整步骤顺序,先安装.NET SDK再执行NuGet还原,确保依赖环境正确:

steps:
# 先安装.NET 6 SDK
- task: UseDotNet@2
  displayName: 'Use .NET Core SDK 6.0'
  inputs:
    version: 6.x

- task: NuGetToolInstaller@1
  displayName: 'NuGet Installer'

- task: NuGetCommand@2
  displayName: 'NuGet Restore'
  inputs:
    restoreSolution: '$(solution)'

# 再执行构建
- task: DotNetCoreCLI@2
  displayName: 'Build Test Automation Solution'
  inputs:
    command: 'build'
    projects: '$(solution)'
    arguments: '--configuration $(buildConfiguration) --output $(Build.BinariesDirectory)'

- task: PublishPipelineArtifact@1
  displayName: 'Publish Artifacts to Pipeline'
  inputs:
    targetPath: '$(Build.BinariesDirectory)'
    artifact: 'TA'
    publishLocation: 'pipeline'

方案二:配置MSBuild任务使用.NET SDK自带的MSBuild

如果必须保留MSBuild@1任务,可以通过指定MSBuild位置来调用.NET SDK中的版本,修改该任务的输入参数:

- task: MSBuild@1
  displayName: 'Build Test Automation Solution'
  inputs:
    solution: '$(solution)'
    vsVersion: 17.0
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    msbuildArgs: '/p:OutDir=$(Build.BinariesDirectory)'
    # 指定使用.NET SDK的MSBuild
    msbuildLocationMethod: 'version'
    msbuildVersion: '17.0'
    msbuildArchitecture: 'x64'

同样需要确保UseDotNet@2步骤在MSBuild之前执行,保证.NET 6 SDK已安装。

补充说明

本地Visual Studio 2022能正常构建是因为它使用的是Visual Studio自带的MSBuild(基于.NET SDK),而Ubuntu代理上默认的Mono MSBuild不支持.NET 6的目标框架,因此必须切换到正确的构建工具版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 01:40:08