Worker服务PR流水线遇NU1100错误:Azure DevOps NuGet包无法解析
问题:Worker服务PR流水线还原Azure DevOps NuGet包时出现NU1100错误
我们有一个Azure DevOps托管的NuGet包,在某.NET API服务中可以正常使用,但将该包添加到Worker服务后,PR流水线的“还原依赖项”步骤抛出NU1100错误。另一使用相同包的服务通过NuGetCommand@2执行还原操作可正常工作,但该方案在此Worker服务中因MSBuild版本问题无法生效,现寻求解决NU1100错误的方案。
流水线文件
trigger: branches: exclude: - '*' paths: include: - '*' pr: branches: include: - '*' pool: vmImage: ubuntu-latest variables: - name: vmImageName value: 'ubuntu-latest' stages: - stage: PR displayName: Build and test jobs: - job: Build displayName: Build pool: vmImage: $(vmImageName) steps: - task: UseDotNet@2 displayName: 'Install .NET Core SDK' inputs: packageType: 'sdk' version: '8.x' performMultiLevelLookup: true - task: NuGetToolInstaller@1 displayName: 'Install NuGet' inputs: versionSpec: '6.8.0' - task: NuGetAuthenticate@1 displayName: 'NuGet Authenticate' - task: DotNetCoreCLI@2 displayName: 'Restore Dependencies' inputs: command: 'restore' projects: '**/*.csproj' feedsToUse: 'config' nugetConfigPath: 'nuget.config' includeNuGetOrg: true noCache: true - task: DotNetCoreCLI@2 displayName: 'Build' inputs: command: 'build' projects: '**/*.csproj' arguments: '--configuration $(buildConfiguration) --no-restore' - job: Test displayName: Test pool: vmImage: $(vmImageName) steps: - task: NuGetAuthenticate@1 displayName: 'NuGet Authenticate' - task: DotNetCoreCLI@2 displayName: "Run Unit Tests" inputs: command: test projects: '**/*.UnitTests.csproj' arguments: '--configuration $(buildConfiguration)'
nuget.config配置
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageRestore> <add key="enabled" value="True" /> <add key="automatic" value="True" /> </packageRestore> <packageSources> <clear /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> <add key="Custom" value="https://pkgs.dev.azure.com/acme/guid/_packaging/Custom/nuget/v3/index.json" /> </packageSources> <packageSourceCredentials> <NxPro> <add key="Username" value="build" /> <add key="ClearTextPassword" value="%PAT%" /> </NxPro> </packageSourceCredentials> <packageSourceMapping> <packageSource key="Custom"> <package pattern="Custom.*" /> </packageSource> <packageSource key="nuget.org"> <package pattern="*" /> </packageSource> </packageSourceMapping> </configuration>
错误信息
Build FAILED. "/home/vsts/work/1/s/src/Infrastructure/ServiceWorker.Infrastructure.csproj" (Restore target) (1) -> (Restore target) -> /home/vsts/work/1/s/src/Infrastructure/ServiceWorker.Infrastructure.csproj : error NU1100: Unable to resolve 'Custom.Contracts (>= 1.0.31)' for 'net8.0'. PackageSourceMapping is enabled, the following source(s) were not considered: feed-Custom, nuget.org. 0 Warning(s) 1 Error(s) Time Elapsed 00:00:07.64 ##[error]Error: The process '/opt/hostedtoolcache/dotnet/dotnet' failed with exit code 1 ##[error]Packages failed to restore Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://docs.microsoft.com/en-us/dotnet/core/tools/ and https://docs.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting Finishing: Restore Dependencies
解决方案
1. 修复nuget.config中的凭据配置错误
nuget.config里的packageSourceCredentials节点名称是<NxPro>,但自定义源的key是Custom,两者不匹配导致无法正确认证到自定义NuGet源,这是核心问题。修改节点名称与自定义源key一致:
<packageSourceCredentials> <Custom> <add key="Username" value="build" /> <add key="ClearTextPassword" value="%PAT%" /> </Custom> </packageSourceCredentials>
2. 调整DotNetCoreCLI还原步骤参数
去掉还原步骤中的includeNuGetOrg: true参数,因为nuget.config已配置nuget.org源,该参数可能覆盖配置导致源加载异常:
- task: DotNetCoreCLI@2 displayName: 'Restore Dependencies' inputs: command: 'restore' projects: '**/*.csproj' feedsToUse: 'config' nugetConfigPath: 'nuget.config' noCache: true
3. 验证PAT权限
确认Azure DevOps的PAT拥有Packaging Read权限,能正常访问Custom NuGet feed,避免因权限不足导致包无法拉取。
4. 排查Package Source Mapping(可选)
如果修复上述问题后仍报错,可暂时注释packageSourceMapping节点,测试是否能正常还原,确认是否为映射规则异常导致的问题。
内容的提问来源于stack exchange,提问作者IbrahimD
相关产品推荐
相关产品推荐

