Azure Pipelines构建报错NU1202:包版本与.NET 6不兼容求助
解决Azure Pipelines中.NET 6项目包还原兼容性错误
问题背景
我是Azure DevOps新手,希望通过Azure Pipelines构建项目,我的YAML配置如下:
- task: JFrogToolsInstaller@1 displayName: 'Installation de JFrogTools' inputs: artifactoryConnection: 'JFrog-V2' cliInstallationRepo: 'jfrogcliv2' - task: JFrogNuGet@1 displayName: 'JfrogNuget Restore' inputs: command: 'restore' artifactoryConnection: 'JFrog-V2' targetResolveRepo: 'nuget-remote' solutionPath: '$(Build.SourcesDirectory)/$(Project_name)/xxx.xxx.sln' collectBuildInfo: true buildName: '$(Build.DefinitionName)' buildNumber: '$(Build.BuildNumber)' - task: DotNetCoreCLI@2 displayName: 'Restore .NET Core' inputs: command: 'restore' projects: '**/*.csproj' feedsToUse: 'select' includeNuGetOrg: false - task: DotNetCoreCLI@2 displayName: 'Build' inputs: command: 'build' projects: '**/*.csproj' arguments: '--configuration Release'
当前执行时出现如下错误:
Exécution de la tâche "RestoreTask" terminée -- ÉCHEC. 1>Génération de la cible "Restore" terminée dans le projet "xxxxx.Caching.csproj" -- ÉCHEC. 1>Génération du projet "C:\DevOpsAgent\_work\385\s\xxxxxx\xxxxx.Caching\xxxx.Caching.csproj" terminée (Restore cible(s)) -- ÉCHEC. ÉCHEC de la build. "C:\DevOpsAgent\_work\385\s\xxxxxx\xxxxx.Caching\xxxxx.Caching.csproj" (Restore cible) (1) -> (Restore cible) -> C:\DevOpsAgent\_work\385\s\xxxxx\xxxxx.Caching\xxxxx.Caching.csproj : error NU1202: The package Microsoft.AspNetCore.SpaServices 3.1.29 it's not compatible with net60 (.NETFramework,Version=v6.0). The package Microsoft.AspNetCore.SpaServices 3.1.29 supports : netcoreapp3.1 (.NETCoreApp,Version=v3.1) 0 Avertissement(s) 1 Erreur(s) Temps écoulé 00:00:01.32 ##[debug]Exit code 1 received from tool 'C:\Program Files\dotnet\dotnet.exe' ##[debug]STDIO streams have closed for tool 'C:\Program Files\dotnet\dotnet.exe' ##[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1 ##[debug]Processed: ##vso[task.issue type=error;]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1 ##[debug]task result: Failed ##[error]Packages failed to restore ##[debug]Processed: ##vso[task.issue type=error;]Packages failed to restore ##[debug]Processed: ##vso[task.complete result=Failed;]Packages failed to restore
我添加了额外任务但构建仍然失败,请问该如何解决?
解决方案
1. 修复包兼容性问题
错误核心是Microsoft.AspNetCore.SpaServices 3.1.29仅支持.NET Core 3.1,但你的项目目标框架是.NET 6,按以下步骤处理:
- 打开
xxxxx.Caching.csproj文件,确认TargetFramework节点值为net6.0 - 卸载旧的
Microsoft.AspNetCore.SpaServices包,安装兼容.NET 6的替代包:.NET 6官方推荐使用Microsoft.AspNetCore.SpaServices.Extensions(选择6.0.x系列版本)
2. 优化Pipeline任务配置
当前YAML存在重复还原任务(JFrogNuGet Restore和DotNetCoreCLI Restore),易引发冲突,调整如下:
- 保留JFrogNuGet@1的还原任务,移除
DotNetCoreCLI@2的Restore任务 - 在Build任务中添加
--no-restore参数,避免自动触发还原,确保使用JFrog完成的还原结果
调整后的YAML示例:
- task: JFrogToolsInstaller@1 displayName: 'Installation de JFrogTools' inputs: artifactoryConnection: 'JFrog-V2' cliInstallationRepo: 'jfrogcliv2' - task: JFrogNuGet@1 displayName: 'JfrogNuget Restore' inputs: command: 'restore' artifactoryConnection: 'JFrog-V2' targetResolveRepo: 'nuget-remote' solutionPath: '$(Build.SourcesDirectory)/$(Project_name)/xxx.xxx.sln' collectBuildInfo: true buildName: '$(Build.DefinitionName)' buildNumber: '$(Build.BuildNumber)' - task: DotNetCoreCLI@2 displayName: 'Build' inputs: command: 'build' projects: '**/*.csproj' arguments: '--configuration Release --no-restore'
3. 验证代理环境的.NET版本
确保Azure DevOps代理已安装.NET 6 SDK,可在Pipeline开头添加检查任务:
- task: UseDotNet@2 displayName: '使用.NET 6 SDK' inputs: packageType: 'sdk' version: '6.x'
内容的提问来源于stack exchange,提问作者xfusion
相关产品推荐
相关产品推荐

