简体   繁体   English

Visual Studio 在 Visual Studio For Mac 2022 上未使用正确的 .NET SDK

[英]Visual Studio Not Using Correct .NET SDK on Visual Studio For Mac 2022

I am attempting to upgrade a project from .net 5 to .net 6我正在尝试将项目从 .net 5 升级到 .net 6

I am using Visual Studio for Mac 2022 v17.3 (I have tried Visual Studio for Mac Preview 2022 v17.4 as well but no luck there)我正在使用 Visual Studio for Mac 2022 v17.3(我也尝试过 Visual Studio for Mac Preview 2022 v17.4,但没有成功)

I have installed the .net 6 sdk onto machine.我已经在机器上安装了 .net 6 sdk。 The output of do.net --list-sdks is: do.net --list-sdks的output是:

dotnet --list-sdks
2.2.100 [/usr/local/share/dotnet/sdk]
3.0.101 [/usr/local/share/dotnet/sdk]
3.1.301 [/usr/local/share/dotnet/sdk]
3.1.419 [/usr/local/share/dotnet/sdk]
3.1.422 [/usr/local/share/dotnet/sdk]
5.0.100 [/usr/local/share/dotnet/sdk]
5.0.408 [/usr/local/share/dotnet/sdk]
6.0.300 [/usr/local/share/dotnet/sdk]
6.0.400 [/usr/local/share/dotnet/sdk]

The output of do.net --version is: do.net --version的 output 是:

dotnet --version
6.0.400

I have attempted both with and without a global.json.我尝试过使用和不使用 global.json。 The contents of the global.json when I tried it:我尝试时 global.json 的内容:

{
  "sdk": {
    "version": "6.0.400"
  }
}

I also tried my other 6 sdk version,我也试过我的其他 6 sdk 版本,

{
  "sdk": {
    "version": "6.0.300"
  }
}

My TargetFramework for my projects is set to.net6.0我项目的 TargetFramework 设置为.net6.0

    <TargetFramework>net6.0</TargetFramework>

Despite all of this, here is the output when I attempt to build from Visual Studio: (building from command line works)尽管如此,当我尝试从 Visual Studio 构建时,这里是 output:(从命令行构建有效)

/usr/local/share/do.net/sdk/ 5.0.408 /Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(141,5): error.NETSDK1045: The current .NET SDK does not support targeting .NET 6.0. /usr/local/share/do.net/sdk/ 5.0.408 /Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(141,5): error.NETSDK1045: 当前.NET SDK没有支持定位 .NET 6.0。 Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports .NET 6.0.要么以 .NET 5.0 或更低版本为目标,要么使用支持 .NET 6.0 的 .NET SDK 版本。

(bold emphasis is my own, on the sdk version) (加粗强调是我自己的,在sdk版本上)

It is only ever trying to use 5.0.408它只会尝试使用 5.0.408

I know there are very many questions and answers related to this issue on stack overflow, one example of which is this question , the only problem is the solutions provided in the other similar questions are not resolving my issue.我知道有很多关于堆栈溢出问题的问题和答案,其中一个例子就是这个问题,唯一的问题是其他类似问题中提供的解决方案并没有解决我的问题。 There seems to be some sort of caching happening and I cannot figure it out.似乎发生了某种缓存,我无法弄清楚。 I just want Visual Studio to use the proper SDK version.我只希望 Visual Studio 使用正确的 SDK 版本。

If I run do.net build MySolution.sln from the command line, it builds using the proper SDK. Visual Studio, on the other hand, does not.如果我从命令行运行do.net build MySolution.sln ,它会使用正确的 SDK 进行构建。另一方面,Visual Studio 不会。 It only uses 5.0.408.它只使用 5.0.408。 There is not a global.json anywhere in my file structure that points to a.net 5 sdk. There are no projects in my solution that Target 5, they all target 6.在我的文件结构中没有指向 a.net 5 sdk 的任何地方的 global.json。我的解决方案中没有目标 5 的项目,它们都以 6 为目标。

I managed to workaround it by:我设法通过以下方式解决它:

  • removing 5.0.408 from my machine从我的机器中删除 5.0.408
  • removing 6.0.400 from my machine从我的机器中删除 6.0.400
  • downloading a fresh install of 5.0.408下载全新安装的 5.0.408
  • downloading a fresh install of 6.0.400下载全新安装的 6.0.400

If this doesnt work, I found another solution which is to uncheck "Build with MSBuild on mono" in the solution build properties.如果这不起作用,我找到了另一个解决方案,即在解决方案构建属性中取消选中“在单声道上使用 MSBuild 构建”。 (Right click solution file, go to properties, "Build > General", uncheck) (右击解决方案文件,go到属性,“Build > General”,取消勾选)

See if this documentation helps.看看这个文档是否有帮助。 Change TargetFramework VisualStudio . 更改 TargetFramework VisualStudio Visual Studio will try to build the project with the target framework you indicate. Visual Studio 将尝试使用您指定的目标框架构建项目。

I've tried to do like you but this doesn't work.我试过像你那样做,但这行不通。

I've removed the SDK 5.0.408 and 6.0.401 like this:我已经像这样删除了 SDK 5.0.408 和 6.0.401:

version="5.0.408"
sudo rm -rf /usr/local/share/dotnet/sdk/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.NETCore.App/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/$version
sudo rm -rf /usr/local/share/dotnet/host/fxr/$version

version="6.0.401"
sudo rm -rf /usr/local/share/dotnet/sdk/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.NETCore.App/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.AspNetCore.All/$version
sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/$version
sudo rm -rf /usr/local/share/dotnet/host/fxr/$version`

Then I've reinstalled the SDK 5.0.408 and 6.0.401.然后我重新安装了 SDK 5.0.408 和 6.0.401。

But I always have the same error when I try to restore packages from Visual Studio.但是当我尝试从 Visual Studio 恢复包时,我总是遇到同样的错误。

Is there something else?还有别的吗?

Inside of Visual Studio 2022 for Mac, I went to Tools, then SDK manager, and set this and it automatically detected the SDK after selecting this file:在 Visual Studio 2022 for Mac 中,我转到“工具”,然后是“SDK 管理器”,然后进行设置,它会在选择此文件后自动检测到 SDK:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM