".NET Framework, Version=v4.7.1"的参考程序集未找到。

43

我试图在Linux(Ubuntu 18.10)下用VSCode打开一个Unity3D项目。然而,Omnisharp扩展程序无法加载该项目,显示找不到程序集的错误。

这可能看起来很愚蠢,但我对.Net并不是很熟悉,而且我已经因为这个错误卡了好几天了。我已经尝试重新安装dotnet(甚至使用snap包)、mono、vscode和omnisharp扩展程序。

所以,任何帮助都是受欢迎的!:)

Unity版本:2018.3.1f1

日志:

dotnet -- info

.NET Core SDK (reflecting any global.json):
 Version:   2.2.103
 Commit:    8edbc2570a

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  18.10
 OS Platform: Linux
 RID:         ubuntu.18.10-x64
 Base Path:   /usr/share/dotnet/sdk/2.2.103/

Host (useful for support):
  Version: 2.2.1
  Commit:  878dd11e62

.NET Core SDKs installed:
  2.2.103 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.2.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.2.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

mono --version

(该命令用于显示Mono运行时的版本号)
Mono JIT compiler version 4.6.2 (Debian 4.6.2.7+dfsg-1ubuntu1)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
    TLS:           __thread
    SIGSEGV:       altstack
    Notifications: epoll
    Architecture:  amd64
    Disabled:      none
    Misc:          softdebug 
    LLVM:          supported, not enabled.
    GC:            sgen

完整的错误日志:

    Starting OmniSharp server at 1/26/2019, 7:13:55 PM
    Target: /home/eyap/Projects/Unity/TestProject/TestProject.sln

OmniSharp server started.
    Path: /home/eyap/.vscode/extensions/ms-vscode.csharp-1.17.1/.omnisharp/1.32.8/run
    PID: 30136

[info]: OmniSharp.Stdio.Host
        Starting OmniSharp on ubuntu 18.10 (x64)
[info]: OmniSharp.Services.DotNetCliService
        DotNetPath set to dotnet
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 1 MSBuild instance(s)
            1: StandAlone 15.0 - "/home/eyap/.vscode/extensions/ms-vscode.csharp-1.17.1/.omnisharp/1.32.8/omnisharp/msbuild/15.0/Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        MSBUILD_EXE_PATH environment variable set to '/home/eyap/.vscode/extensions/ms-vscode.csharp-1.17.1/.omnisharp/1.32.8/omnisharp/msbuild/15.0/Bin/MSBuild.dll'
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: StandAlone 15.0 - "/home/eyap/.vscode/extensions/ms-vscode.csharp-1.17.1/.omnisharp/1.32.8/omnisharp/msbuild/15.0/Bin"
            MSBuildExtensionsPath = /home/eyap/.vscode/extensions/ms-vscode.csharp-1.17.1/.omnisharp/1.32.8/omnisharp/msbuild
            BypassFrameworkInstallChecks = true
            CscToolPath = /home/eyap/.vscode/extensions/ms-vscode.csharp-1.17.1/.omnisharp/1.32.8/omnisharp/msbuild/15.0/Bin/Roslyn
            CscToolExe = csc.exe
            MSBuildToolsPath = /home/eyap/.vscode/extensions/ms-vscode.csharp-1.17.1/.omnisharp/1.32.8/omnisharp/msbuild/15.0/Bin
            TargetFrameworkRootPath = /usr/lib/mono/xbuild-frameworks
[info]: OmniSharp.Cake.CakeProjectSystem
        Detecting Cake files in '/home/eyap/Projects/Unity/TestProject'.
[info]: OmniSharp.Cake.CakeProjectSystem
        Could not find any Cake files
[info]: OmniSharp.WorkspaceInitializer
        Project system 'OmniSharp.DotNet.DotNetProjectSystem' is disabled in the configuration.
[info]: OmniSharp.MSBuild.ProjectSystem
        Detecting projects in '/home/eyap/Projects/Unity/TestProject/TestProject.sln'.
[info]: OmniSharp.MSBuild.ProjectManager
        Queue project update for '/home/eyap/Projects/Unity/TestProject/Assembly-CSharp.csproj'
[info]: OmniSharp.Script.ScriptProjectSystem
        Detecting CSX files in '/home/eyap/Projects/Unity/TestProject'.
[info]: OmniSharp.Script.ScriptProjectSystem
        Could not find any CSX files
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpWorkspaceOptionsProvider
[info]: OmniSharp.WorkspaceInitializer
        Configuration finished.
[info]: OmniSharp.Stdio.Host
        Omnisharp server running using Stdio at location '/home/eyap/Projects/Unity/TestProject' on host 30056.
[info]: OmniSharp.MSBuild.ProjectManager
        Loading project: /home/eyap/Projects/Unity/TestProject/Assembly-CSharp.csproj
[fail]: OmniSharp.MSBuild.ProjectLoader
        The reference assemblies for framework ".NETFramework,Version=v4.7.1" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.
[warn]: OmniSharp.MSBuild.ProjectManager
        Failed to load project file '/home/eyap/Projects/Unity/TestProject/Assembly-CSharp.csproj'.
/home/eyap/Projects/Unity/TestProject/Assembly-CSharp.csproj
/home/eyap/.vscode/extensions/ms-vscode.csharp-1.17.1/.omnisharp/1.32.8/omnisharp/msbuild/15.0/Bin/Microsoft.Common.CurrentVersion.targets(1195,5): Error: The reference assemblies for framework ".NETFramework,Version=v4.7.1" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.

[fail]: OmniSharp.MSBuild.ProjectManager
        Attemped to update project that is not loaded: /home/eyap/Projects/Unity/TestProject/Assembly-CSharp.csproj

对于未来的观众: 我决定保留被接受的答案,因为那是在当时让它对我起作用的答案。但是,请查看得分最高的答案,以获取更近期的解决方案。 - Eyap
9个回答

47
尝试了此处提到的步骤,未能解决错误。必须将omnisharp.useGlobalMono选项更改为always。这适用于C#扩展程序VS Code=1.23.2
来源:Reddit帖子

Your error output tells me that you're using the official C# extension for VS Code. The latest version of said extension, 1.23.2, has this to say in its changelog:

For Mono-based development (e.g. Unity) that requires full .NET framework, you need to set "omnisharp.useGlobalMono": "always". This

is needed until Mono upgrades their bundled MSBuild version

The default setting, auto, does not work. Switch it to always and restart the extension. Alternatively you could downgrade to 1.23.1.


1
这在我的VSCode 1.49.1、Unity 2020.1.6f1 和Mono 6.10.0 Stable上运行良好,无需修改PATH或任何环境变量。 - Felipe
终于在这一步之后,我的Intellisense像魔法般地正常工作了。这绝对让我开心了一整天,谢谢! - J-Cint
3
在Mac电脑上,打开“Code”应用程序,点击“偏好设置”,选择“设置”选项,在搜索框中输入“use global mono”,更新配置文件以始终使用该选项。 - BatteryAcid
1
这对我在 Windows 10 上没有解决,我仍然得到“未找到 .NETFramework,Version=v4.7.1 的引用程序集。” :( - A.Crane
@Paresh 你是最有价值的球员 (MVP)。 - Jimmie Tyrrell
显示剩余6条评论

31

我遇到了同样的问题,但是在OSX上。安装最新版本的Mono可以解决缺失的.net v4.7.1框架引用问题。

我从https://www.mono-project.com/download/stable安装了Mono V5.18.0。

更新:请参见下面的评论关于全局omnisharp设置的说明


可能就是这样!我之前按照他们网站上的描述安装了mono,但我错误地认为它已经是最新版本了。但对于Ubuntu 18.10来说并非如此(仍然是4.6)...我明天会尝试一下,非常感谢! - Eyap
这对我有用。我猜最后一次我安装的Unity更新将所需版本号提高到了Ubuntu Apt源提供的版本之上。 - slembcke
"sudo apt upgrade mono-devel"。当前最新版本是6.10。 - dolgom
3
我拥有最新的Mono,但仍然遇到这个问题。 - Aaron Franke
1
可以确认这在Ubuntu 18.04上工作。非常感谢! - Bono

27

谢谢你的回答。在我的 macOS 上有效。我已经将该变量添加到我的 shell 配置中。 - Yevhen
7
你需要在哪里输入那个导出命令?Unity或VS Code如何知道在哪里找到这个导出命令? - tothemario
8
这对我有用:export FrameworkPathOverride=/Library/Frameworks/Mono.framework/Versions/Current - Max
这正是问题所在,你救了我的一天,谢谢。 - Kennir

13

我曾经遇到相同的问题,这里是我如何解决它的方法。

  1. 这里 下载最新版本的稳定版 Mono 并安装。

  2. 将 FrameworkPathOverride 添加到 .bash_profile 中。

export FrameworkPathOverride=/Library/Frameworks/Mono.framework/Versions/Current

重新启动VSCode,它就可以正常工作了。


4

针对Windows 10用户:


如果你仍然遇到Omnisharp错误:

The reference assemblies for .NETFramework,Version=v4.7.1 were not found.
  1. 如果已经打开了VS Code,请关闭它。

  2. 下载并安装.NET Framework 4.7.1 Developer Pack
    https://dotnet.microsoft.com/en-us/download/dotnet-framework/net471
    (VS Code可能会建议使用更新版本,但截至2022年3月25日,这些版本不可用)

  3. 重新在VS Code中打开Unity项目。


上述步骤应该可以解决问题,但如果仍然出现问题(例如Intellisense无法正常工作),请尝试调整您的Unity设置:

  1. 打开您的Unity项目,单击编辑>项目设置...>玩家>其他设置>API兼容性级别(在配置下方)

  2. 将API兼容性级别更改为.NET 4.x

  3. 重新启动Unity和VS Code。


4

安装最新的 mono 没有帮助我解决问题。但是导出命令修复了问题,谢谢! - Anubis

4
在Linux Mint 18.3 Sylvia(Ubuntu 16.04)中,安装mono-devel的方法如下:https://www.mono-project.com/download/stable/#download-lin。然后在终端中执行以下命令:
export FrameworkPathOverride=/etc/mono/4.5

打开VScode,它就能工作了。这是我在干净的Ubuntu 20.04安装上使用的方法。


0

0

在更新的OmniSharp版本中(用于.NET 6开发),需要禁用“Omnisharp:使用现代Net”选项。 可以仅更改本地工作区设置,以保持您的非Unity .NET Core项目不受影响。

VS Code文件>首选项>设置(可选:>工作区标签)>Omnisharp:使用modern net

或者更改.vscode/settings.json文件,在您的home文件夹(全局设置)或项目工作区内(本地设置)。

.vscode/settings.json: { [...] "omnisharp.useModernNet": false, [...] }


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