VS Code的C#扩展存在问题,由于OmniSharp的原因,一些项目无法加载

20

当我开始编辑一个C#文件时,会收到这个警告消息

Some projects have trouble loading. Please review the output for more details.

我检查了输出结果,发现如下信息

Starting OmniSharp server at 2/17/2021, 6:06:51 AM
    Target: /Users/zeyad/Documents/Sandbox/CSharpTut

OmniSharp server started with Mono 6.12.0.
    Path: /Users/zeyad/.vscode/extensions/ms-dotnettools.csharp-1.23.9/.omnisharp/1.37.7-beta.29/omnisharp/OmniSharp.exe
    PID: 1391

[info]: OmniSharp.Stdio.Host
        Starting OmniSharp on MacOS 10.13.6 (x64)
[info]: OmniSharp.Services.DotNetCliService
        DotNetPath set to dotnet
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 2 MSBuild instance(s)
            1: Mono 16.6.0 - "/Library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/msbuild/Current/bin"
            2: StandAlone 16.8.0 - "/Users/zeyad/.vscode/extensions/ms-dotnettools.csharp-1.23.9/.omnisharp/1.37.7-beta.29/omnisharp/.msbuild/Current/Bin"
[warn]: OmniSharp.CompositionHostBuilder
        It looks like you have Mono installed which contains a MSBuild lower than 16.8.0 which is the minimum supported by the configured .NET Core Sdk.
 Try updating Mono to the latest stable or preview version to enable better .NET Core Sdk support.
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: Mono 16.6.0 - "/Library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/msbuild/Current/bin"
            CscToolExe = csc.exe
            CscToolPath = /Users/zeyad/.vscode/extensions/ms-dotnettools.csharp-1.23.9/.omnisharp/1.37.7-beta.29/omnisharp/.msbuild/Current/Bin/Roslyn
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionOptionsProvider, Order: 0
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.RenameWorkspaceOptionsProvider, Order: 100
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.ImplementTypeWorkspaceOptionsProvider, Order: 110
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.BlockStructureWorkspaceOptionsProvider, Order: 140
[info]: OmniSharp.Cake.CakeProjectSystem
        Detecting Cake files in '/Users/zeyad/Documents/Sandbox/CSharpTut'.
[info]: OmniSharp.Cake.CakeProjectSystem
        Could not find any Cake files
[info]: OmniSharp.MSBuild.ProjectSystem
        No solution files found in '/Users/zeyad/Documents/Sandbox/CSharpTut'
[info]: OmniSharp.MSBuild.ProjectManager
        Queue project update for '/Users/zeyad/Documents/Sandbox/CSharpTut/Zeyad.csproj'
[info]: OmniSharp.Script.ScriptProjectSystem
        Detecting CSX files in '/Users/zeyad/Documents/Sandbox/CSharpTut'.
[info]: OmniSharp.Script.ScriptProjectSystem
        Could not find any CSX files
[info]: OmniSharp.WorkspaceInitializer
        Configuration finished.
[info]: OmniSharp.Stdio.Host
        Omnisharp server running using Stdio at location '/Users/zeyad/Documents/Sandbox/CSharpTut' on host 1365.
[info]: OmniSharp.MSBuild.ProjectManager
        Loading project: /Users/zeyad/Documents/Sandbox/CSharpTut/Zeyad.csproj
[fail]: OmniSharp.MSBuild.ProjectLoader
        This project targets .NET version that requires reference assemblies that do not ship with OmniSharp out of the box (e.g. .NET Framework). The most common solution is to make sure Mono is installed on your machine (https://mono-project.com/download/) and that OmniSharp is started with that Mono installation (e.g. 'omnisharp.useGlobalMono':'always' in C# Extension for VS Code).
[warn]: OmniSharp.MSBuild.ProjectManager
        Failed to load project file '/Users/zeyad/Documents/Sandbox/CSharpTut/Zeyad.csproj'.
/Users/zeyad/Documents/Sandbox/CSharpTut/Zeyad.csproj
/Library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/msbuild/Current/bin/Microsoft.Common.CurrentVersion.targets(1193,5): Error: This project targets .NET version that requires reference assemblies that do not ship with OmniSharp out of the box (e.g. .NET Framework). The most common solution is to make sure Mono is installed on your machine (https://mono-project.com/download/) and that OmniSharp is started with that Mono installation (e.g. 'omnisharp.useGlobalMono':'always' in C# Extension for VS Code).

[fail]: OmniSharp.MSBuild.ProjectManager
        Attempted to update project that is not loaded: /Users/zeyad/Documents/Sandbox/CSharpTut/Zeyad.csproj
[info]: OmniSharp.Roslyn.CSharp.Services.Diagnostics.CSharpDiagnosticWorkerWithAnalyzers
        Solution initialized -> queue all documents for code analysis. Initial document count: 0.

我检查了他们写的解决方案solution is to make sure Mono is installed on your machine (https://mono-project.com/download/) and that OmniSharp is started with that Mono installation (e.g. 'omnisharp.useGlobalMono':'always' in C# Extension for VS Code). 它解决了问题,但第二天问题又出现了!

这是我在我的settings.json中添加的内容。

"omnisharp.path": "latest",
"omnisharp.useGlobalMono": "always"

我对C#的扩展是C#扩展和Roslynator。

为了完全设置Visual Studio Code来使用C#,是否还有其他东西需要添加? 我对C#完全是个新手,希望能得到任何帮助使其像JavaScript或Golang等其他编程语言一样顺畅。

注意:我使用的是Mac,但这会有什么区别吗?


1
听起来你需要更新Mono。 - Aluan Haddad
我有6.12.0.90版本,我认为这是最新的,但我应该选择哪个包?Visual Studio频道还是稳定频道? - Zeyad Shaban
解决了一个错误后,我又遇到了另一个错误。现在 Visual Studio Code 将每一行都标记为错误,例如 using System The type or namespace name 'System' could not be found。我尝试重新启动 OmniSharp 并尽我所能做的一切,但每当我修复一个错误时,我就会遇到另一个错误。为什么我不能像其他语言那样正常编写 C# 代码呢?我需要使用 Windows 而不是 Mac 吗?我需要从 vs code 迁移吗? - Zeyad Shaban
它应该在Mac上正常工作。但是你需要提出一个单独的问题,因为现在是不同的问题。 - Aluan Haddad
还可以查看Visual Studio for Mac - Aluan Haddad
显示剩余2条评论
11个回答

34

按照以下步骤修复:

  1. https://mono-project.com/download/中更新Mac上的Mono
  2. VS Code > C#扩展程序(已安装)> 扩展程序设置 > 搜索:omnisharp.useGlobalMono':'always' > 将选项设置为Always

好建议。但它仍然没有解决问题。我会等待OmniSharp扩展的未来版本。 - Mukus
谢谢,我在Ubuntu 20.4上遇到了问题,这个解决了它。我差点要回到Windows了:( - Deccoyi
2
安装“预览版”后,问题已得到解决,而不是“稳定版”。 - mervasdayi
老兄,我真的很喜欢你,谢谢,这个解决方案在新的Macbook Pro Silicon上运行良好! - Maurizio Battaghini
1
不再适用于 OmniSharp > 1.25,请查看 Airinfinite 的答案。 - Erik Sillén
显示剩余2条评论

29

解决这个问题有两种方法。

第一种方法: 检查您的C#扩展版本。如果您使用的是1.25.0版本,则需要前往扩展设置,将omnisharp.useModernNet设置为false

第二种方法: 如果您使用的是1.25.0版本,请将其还原为1.24.0版本。确保将omnisharp.useGlobalMono设置为always


4
omnisharp.useModernNet 设置为 false 完美地起作用了。我确实需要重新启动 VS Code 才能生效。 - jaxoncreed
2
这适用于1.25.0版本!对于Mac用户:1)进入用户文件夹并按下Cmd + Shift + .以显示隐藏的Library文件夹;2)进入Library/Application Support/Code/User/settings.json;3)添加"omnisharp.useModernNet": false;4)重新启动VS Code,您将在C#日志中看到它安装了“OmniSharp for OSX (Mono / x64)”。 - Aidan Wolf
在Ubuntu上,将userModernNet设置为false,并在从Microsoft安装dotnet-sdk后手动安装mono-devel https://www.mono-project.com/download/stable/,可以使VSCode正确安装Omnisharp并且一切似乎都正常工作... - gordon_freeman
现在看来,这似乎是版本1.25.x的正确答案。 - shoopi

16

这对我有用:

  1. 安装最新的C#扩展
  2. 安装最新的Mono for MAC:https://mono-project.com/download/
  3. 将VSCode ~/Library/Application Support/Code/User/settings.json 更新如下:
   {
    ...
    "omnisharp.useGlobalMono": "always",
    "omnisharp.monoPath": "/Library/Frameworks/Mono.framework/Versions/Current"
   }

我不需要 "omnisharp.path": "latest",因为我不想让扩展程序每次检查更新。


1
对我有用。谢谢@Ken - Akash Chaudhary
非常感谢!作为一名经验丰富的开发人员,来自完全不同技术领域,你帮助我避免了因本应如此基础的事情而变得疯狂 :) - jipiboily
如果你发现 useGlobalMono 被弃用了,那可能是因为你使用的插件版本太新了。可以查看 Airinfinite 的回答。 - Erik Sillén

11

对于那些来自较近时期的人,需要提醒一下。Omnisharp在最新版本中弃用了useGlobalMono变量。花费了我很多时间,但是一旦将Omnisharp恢复到1.24.0(从1.25.0),它就可以正常工作了。


我还没有采取这种方法,但我也遇到了问题...已经尝试了其他步骤。 - Rusty Parks
3
在1.25.0版本中,将omnisharp.useModernNet设置为false,正如Airinfinite在他的回答中提到的那样。 - AW101

3
我在运行MacOS 12.4的MacBook Pro上遇到了同样的问题。
要解决这个问题,我需要:
- 下载并安装dotnet sdk - 下载并安装mono (https://mono-project.com/download/) - 在终端中添加这两个路径 - 运行以下命令:sudo ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet,其中/usr/share/dotnet/dotnet应替换为您所在平台的.NET SDK安装程序的位置 - 在~/.zshrc中添加这两个路径并进行源代码编译 - 在VSCode中添加这两个路径(设置->搜索mono和dotnet),通过运行which mono和which dotnet获取路径。您可以在用户和工作区选项卡中执行此操作。我两个都做了。我还特别设置了sdk路径和版本。 - 在VSCode中关闭useModernNet
其中一些步骤可能是不必要或多余的。但只有在我尝试了所有这些步骤后,我才最终让它正常工作。这个链接真的很有帮助:https://github.com/OmniSharp/omnisharp-vscode/wiki/Troubleshooting:-'The-.NET-Core-SDK-cannot-be-located.'-errors
更新: 我还应该添加,我必须为每个项目重新执行此操作。一个更简单的解决方案是仅安装带有紫色图标的Mac版Visual Studio而不是Visual Studio Code。在那里我什么也不用添加。

非常感谢。我不需要执行步骤4和5。但是步骤6和7(以及前面几个步骤)很关键。 - Weiming Hu
我还应该补充一点,就是我不得不为每个项目重新做这个。一个更简单的解决方案是安装带有紫色图标的Visual Studio for Mac而不是Visual Studio Code。在那里我不需要添加任何东西。 - Mister_CK
我还需要下载Mono并将其添加到我的路径中(注:在C#扩展中,我也设置了“Mono Path”,但我不确定是否必要) - Blundering Philosopher

2

根据这里的评论,将C#扩展程序恢复到1.24.4版本可以解决M1 Mac上的问题。我从C#扩展程序版本1.25.0回退到1.24.4。


1
我正在 MacBook Air M1 上测试与 Unity 的工作。扩展版本为 1.25.4。如 VSCode 中的设置所说,您不能在 Unity 中使用 ModernNet。因此,您需要将其添加到设置中。
"omnisharp.useModernNet": false,

在我的电脑上,我先安装了C#扩展,然后再安装了dotnet和mono。这似乎搞砸了一些东西。当我应用上述设置时,它抱怨找不到一个名为OmniSharp.exe的文件。最后我不得不做的hack是删除扩展目录并重新安装扩展。
rm -rf ~/.vscode/extensions/ms-dotnettools.csharp-1.25.4-darwin-arm64

现在它可以工作了。


1

针对最近版本的Omnisharp,只需按照截图中所示,在VS Code C#扩展设置中取消勾选useModernNet即可。请参考下方图片。 enter image description here


1

目前(2022年11月),M1 Mac仍需要回滚到1.24.4版本。


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community
1
他还需要说什么呢?它起作用了。我听从了他的建议。我已经花了过去4个小时与奇怪的错误消息斗争。现在它们消失了 :P - Hugh Perkins

1

更新:
Mac M1 13.3.1
VSCode 的 C# 扩展: v2.0.320

  • 下载并安装 dotnet SDK
  • 下载并安装 mono (https://mono-project.com/download/)。 (稳定通道)
  • 在 Omnisharp 下关闭 VSCode 中的 useModernNet
  • 在 VSCode 设置的 Omnisharp 下设置 mono 和 dotnet 的路径
  • Mono 路径: /Library/Frameworks/Mono.framework/Versions/Current
  • dotnot 路径: /home/username/dotnet/sdks/7.0.306 (根据实际情况替换用户名和 dotnet 版本号)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接