问题 MSBUILD抛出错误:找不到指定的SDK“Microsoft.NET.Sdk”


我正在尝试使用msbuild命令行构建解决方案,并且我不断收到此错误:

error MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found.

msbuild的版本是microsoft visual studio 2017工具的最新版本。我正在使用Windows Server 2012 R2,该项目使用.NET Core 2.0。

这是我正在使用的命令:

msbuild.exe /p:Configuration=Release /t:restore C:\Projects\MyProject.sln

完整日志:

    Microsoft (R) Build Engine version 15.3.409.57025 for .NET Framework
    Copyright (C) Microsoft Corporation. All rights reserved.

    Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
    Build started 9/16/2017 3:09:03 PM.
    Project "C:\Projects\MyProject.sln" on node 1 (restore target(s)).
    ValidateSolutionConfiguration:
      Building solution configuration "Release|Any CPU".
    Project "C:\Projects\MyProject.sln" (1) is building "C:\Projects\Kernel\Kernel.csproj" (2) on node 1 (restore target(s)).
    C:\Projects\MyProject.sln" (1) is building "C:\Projects\Kernel\Kernel.csproj : error MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found.
    Done Building Project "C:\Projects\MyProject.sln" (1) is building "C:\Projects\Kernel\Kernel.csproj" (restore target(s)) -- FAILED.

    Build FAILED.
    "C:\Projects\MyProject.sln" (restore target) (1) ->
"C:\Projects\Kernel\Kernel.csproj" (restore target) (2) ->
  C:\Projects\Kernel\Kernel.csproj : error MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found.

0 Warning(s)
    11 Error(s)

12341
2017-09-16 18:54


起源

我的猜测是项目引用了SDK。并且为了构建它,必须安装它。编译器还能怎么做typechkes和其他所有东西? - Christopher
@Christopher我用完整的日志编辑了帖子。是的,我的项目引用了SDK,但我正在尝试恢复它。 - Luiz Gustavo Maia
和我想的一样。编译器找不到SDK。您需要将它与编译器一起安装以进行构建。理想情况下,您选择了默认位置。如果没有,您可能必须编辑项目,以便在适当的位置查找SDK。 - Christopher
但我已经安装了.NET Core 2.0 SDK。那么,我是否必须编辑项目文件或者是否需要在安装MSBuild的相同位置重新安装.NET Core SDK? - Luiz Gustavo Maia
它现在有效!我使用了以下命令: C:\Program Files\dotnet\dotnet.exe msbuild - Luiz Gustavo Maia


答案:


在使用.Net Core 2.0安装并且看起来搞砸了之后我遇到了这个错误。我会得到同样的错误 dotnet restoredotnet build 要么 dotnet msbuild。基本上,涉及.Net Core和msbuild的任何事情。

发生错误是因为 MSBuildSDKsPath 环境变量仍然指向旧的.Net Core 1.1 SDK。

要解决这个问题,我手动设置了 MSBuildSDKsPath 环境变量指向2.0.0的SDK路径,对于x64我来说,这是: C:\Program Files\dotnet\sdk\2.0.0\Sdks

基本上,如果你有 Sdk="Microsoft.NET.Sdk" 在你的 .csproj,然后你的名字应该有一个同名的文件夹 MSBuildSDKsPath 位置。


14
2017-09-21 08:32