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

AzureDevOps中DotNetCoreCLI不编译类库却错误从Artifacts查找问题

问题描述

解决方案结构

My Solution
   ClassLibA
   ClassLibB
   MyAspNetCoreApp

其中ClassLibA依赖Azure DevOps Artifacts中mynuget源的GeneralLibX(2.0.0)和GeneralLibY(1.1.0)。

Azure DevOps Pipeline配置

trigger:
- master

pool:
  name: 'Default'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: UseDotNet@2
  displayName: 'Install .NET Core SDK'
  inputs:
    version: 6.x

- task: NuGetToolInstaller@0

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'config'
    nugetConfigPath: './nuget.config'
  
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: 'MyAspNetCoreApp'

nuget.config内容

<configuration>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="mynuget" value="https://www.azuredevops.web/MyCollection/_packaging/mynuget/nuget/v3/index.json" />
  </packageSources>
</configuration>

问题现象

Pipeline的克隆代码、安装.NET SDK、NuGet还原步骤均正常,但执行DotNetCoreCLI的build步骤时出现NU1301错误(无法加载指定源的服务索引),构建陷入无限循环,只能手动取消。核心疑问:为何DotNetCoreCLI不编译解决方案内的ClassLibA,反而尝试从Artifacts NuGet源查找已编译版本?

原因分析
  • 构建范围未包含依赖项目:DotNetCoreCLI@2的build任务仅指定MyAspNetCoreApp为构建目标,未包含其依赖的ClassLibA和ClassLibB。当MSBuild构建AspNetCoreApp时,需要获取ClassLibA的编译输出,但由于ClassLibA未被提前编译,MSBuild错误地将其识别为NuGet包依赖,转而从配置的NuGet源中查找,触发了NU1301错误。
  • 还原与构建的上下文不一致:使用NuGetCommand@2完成还原,后续用DotNetCoreCLI@2执行构建。dotnet build默认会在构建前自动执行还原逻辑,此时可能未正确复用之前NuGetCommand的还原结果,加上仅指定单个项目,导致无法识别解决方案内的项目引用,进而尝试从远程源拉取。
  • Azure Artifacts源权限不足:NU1301错误本身是因为构建代理访问mynuget源时缺少权限(未配置访问令牌),但这暴露了核心问题——本应编译本地项目,却错误触发了远程源的查找。
解决方法
  1. 扩大构建范围至整个解决方案
    修改DotNetCoreCLI@2的build任务,将构建目标改为解决方案文件,确保所有依赖项目都被编译:
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/*.sln'
    arguments: '--configuration $(buildConfiguration)'
  1. 按依赖顺序单独构建项目
    如果仅需构建MyAspNetCoreApp,需先编译其依赖的类库项目:
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: 'ClassLibA/*.csproj'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: 'ClassLibB/*.csproj'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: 'MyAspNetCoreApp/*.csproj'
    arguments: '--configuration $(buildConfiguration)'
  1. 配置Azure Artifacts源访问权限
    在NuGet还原前添加NuGetAuthenticate@0任务,自动注入访问令牌,解决NU1301的权限问题:
- task: NuGetAuthenticate@0
  displayName: 'Authenticate NuGet feeds'

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'config'
    nugetConfigPath: './nuget.config'
  1. 统一使用dotnet命令完成还原与构建
    替换NuGetCommand@2为DotNetCoreCLI@2的restore命令,保持还原和构建的上下文一致:
- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '**/*.sln'
    feedsToUse: 'config'
    nugetConfigPath: './nuget.config'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 04:57:37