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

Azure DevOps中DotNet Test任务还原EnterpriseLibrary.Logging Nuget失败

问题根因

DotNetCoreCLI@2执行test命令时默认会触发隐式还原操作,你没有为该任务指定NuGet配置文件路径,导致该步骤没有读取你定义好的包含nuget.org源的Nuget.config,使用了Azure DevOps默认的源优先级配置,优先从私有源查找公共包,才触发了401未授权错误。

修复方案

  • 方案1(最推荐):关闭test步骤的隐式还原
    你已经在前置步骤完成了全解决方案的还原,构建也执行成功,测试阶段不需要再执行还原操作,直接修改你的YML配置,给test命令添加--no-restore参数即可:

    - task: DotNetCoreCLI@2
      inputs:
        command: 'test'
        projects: |
          $(Build.SourcesDirectory)\TestSln\**\*.csproj
        arguments: '--filter TestCategory=RunOnServerBuild --no-restore'
        testRunTitle: 'TestSln Dot Net Test'
    
  • 方案2:为test任务指定NuGet配置文件
    如果你确实需要在test阶段执行还原,和之前的NuGetCommand@2任务一样,给DotNetCoreCLI@2添加nugetConfigPath配置项,指向你的自定义配置文件:

    - task: DotNetCoreCLI@2
      inputs:
        command: 'test'
        projects: |
          $(Build.SourcesDirectory)\TestSln\**\*.csproj
        arguments: '--filter TestCategory=RunOnServerBuild'
        testRunTitle: 'TestSln Dot Net Test'
        nugetConfigPath: NuGet.config
    
  • 可选优化:配置包源映射
    你可以在Nuget.config中添加包源映射规则,明确指定公共包仅从nuget.org拉取,避免去私有源查询,从根源避免这类401问题,示例配置如下:

    <packageSourceMapping>
      <packageSource key="nuget.org">
        <package pattern="EnterpriseLibrary.*" />
        <package pattern="Microsoft.*" />
        <package pattern="System.*" />
      </packageSource>
      <packageSource key="PrivateFeed1">
        <package pattern="YourOrg.Prefix.*" />
      </packageSource>
      <packageSource key="PrivateFeed2">
        <package pattern="YourOtherOrg.Prefix.*" />
      </packageSource>
    </packageSourceMapping>
    
  • 额外排查点:你可以检查Nuget.config中nuget.org的配置顺序,将其放到packageSources节点的第一位,确保NuGet优先从公共源查找包,减少不必要的私有源查询。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 00:00:05