Azure Pipeline构建.NET 8 RC2 SDK的C#项目失败求助
问题
我们有一个Azure Function应用项目,使用.NET 8 RC2 SDK在本地可正常构建,但通过Azure Pipeline构建时,在NuGet包还原步骤失败,抛出以下错误:
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at NuGet.CommandLine.RestoreCommand.<DetermineInputsFromMSBuildAsync>d__63.MoveNext() ##[error]The nuget command failed with exit code(1) and error(D:\a\1\s\some.API.csproj : error : Could not resolve SDK "Microsoft.NET.Sdk". Exactly one of the probing messages below indicates why we could not resolve the SDK. Investigate and resolve that message to correctly specify the SDK. D:\a\1\s\SOME.API.csproj : error : Unable to locate the .NET SDK. Check that it is installed, your PATH is configured for the correct architecture, and that the version specified in global.json (if any) matches the installed version. D:\a\1\s\SOME.API.csproj : error : The NuGetSdkResolver did not resolve this SDK because there was no version specified in the project or global.json. D:\a\1\s\SOME.API.csproj : error : MSB4236: The default SDK resolver failed to resolve SDK "Microsoft.NET.Sdk" because directory "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk\Sdk" did not exist. D:\a\1\s\SOME.API.csproj : error MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found. No .NET SDKs were found.
在该步骤之前,.NET 8.0.x SDK已成功下载,且Pipeline的“Use .NET Core”任务已启用“include preview versions”选项。请问该错误的原因是什么?
原因分析
- NuGet还原任务未关联已配置的.NET 8 SDK环境:如果Pipeline中使用的是传统
NuGetCommand任务执行还原,它会默认调用系统全局的MSBuild/NuGet工具链,而非Use .NET Core任务配置好的.NET 8预览版SDK环境。传统NuGet任务无法识别预览版SDK的独立安装路径,导致找不到Microsoft.NET.Sdk。 - SDK解析路径不匹配预览版安装位置:错误提示显示默认SDK解析器在Visual Studio 2022的MSBuild目录中查找SDK,但.NET 8 RC2这类预览版SDK不会安装到Visual Studio的默认SDK目录,而是存放在.NET SDK的独立路径下,NuGet任务未指向该路径,因此无法找到对应SDK。
- 未明确约束SDK版本:项目或
global.json中未指定具体的.NET 8 RC2版本号,即使Use .NET Core任务安装了SDK,NuGet仍可能优先尝试查找正式版SDK,无法自动匹配到已安装的预览版,最终导致解析失败。
内容的提问来源于stack exchange,提问作者Anand Sowmithiran
相关产品推荐
相关产品推荐

