Linux 上的 F# - 针对 net4xx

F# on Linux - targeting net4xx

我的 Linux Mint (18.3) 机器上安装了 mono (5.10.1.20) 和 dotnet core (2.1.4)。

我想使用 VS Code Ionide 创建一个项目:Ctrl+Shift+P -> F#:新建项目 -> 控制台。这没有问题。但是,当我尝试构建它时,我得到:

error MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.6.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.

我查看了我的 .fsproj 文件,确实 - 它说:

<TargetFramework>net461</TargetFramework>

但是,我在网上发现了一些文章,其中人们声称能够毫无问题地构建它(例如,在这里找到 Suave-Music-Store 教程:

https://legacy.gitbook.com/book/theimowski/suave-music-store/details)

我肯定漏掉了什么。所以我的问题是:到底是什么(以及如何实现)?

PS:我可以通过将目标框架更改为 "netcoreapp2.0" 来解决这个问题,但我仍然非常确定模板应该开箱即用。

问题是您没有 .NET Framework 参考程序集。编译器需要它们才能了解您的代码引用的 .NET Framework API。

好消息是您可以通过 nuget 获取这些参考程序集

坏消息是它尚未在 nuget.org 上发布,因此您需要将 myget 提要添加到您的项目

要解决此问题,请将以下内容添加到您的 fsproj

<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
    <PackageReference 
          Include="Microsoft.TargetingPack.NETFramework.v4.6.1" 
          Version="1.0.1" ExcludeAssets="All" PrivateAssets="All" />
</ItemGroup>

然后你需要添加一个Nuget.Config文件到你的项目

<configuration>
 <packageSources>
    <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
 </packageSources>
</configuration>

有了这些,您应该能够在 Linux

上构建 net461 exe 和 dll

另一个答案是设置 FrameworkPathOverride MsBuild 参数。您需要的程序集已经包含在 Mono 中,您可以通过设置 FrameworkPathOverride 告诉编译器在哪里可以找到它们。您可以通过在此处包含 netfx.props 文件轻松地做到这一点:https://github.com/fsprojects/FSharp.TypeProviders.SDK/blob/master/netfx.props。您可以将其下载到您的存储库,然后将以下行添加到您的所有项目文件中:

<Import Project="..\netfx.props" />

这是几个项目所做的。一旦 MSBuild 也更改为此处显示,或者出现官方 nuget 包,我希望许多 FSharp 项目切换到其中一种方法。

谢谢大家的回复 - 让我 post 我自己发现的一些东西。 我发现将以下行添加到我的 build.sh 脚本中也可以完成交易: export FrameworkPathOverride=$(dirname $(which mono))/../lib/mono/4.5/ 我想这和 Chester 所说的类似,但方式有点不同。