在 Linux 上使用 Mono 构建 VS 2017 MSBuild csproj 项目

Building VS 2017 MSBuild csproj Projects with Mono on Linux

我有 .NET Core projects I am trying to build using Travis CI on Mac and Linux using the latest Mono and .NET Core 1.0.1 tooling (MSBuild based csproj tooling). They target netstandard1.6.1, net45 and net461. The error I get from Travis CI 是:

/usr/share/dotnet/sdk/1.0.1/Microsoft.Common.CurrentVersion.targets(1111,5): error MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.5" 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.

Mono 不支持基于 VS 2017 MSBuild 的 csproj 项目吗?我怎样才能构建我的项目?

据我所知,这里有两个选项:

  • 使用 FrameworkPathOverride 环境变量,如 this issue 中所述,指向它们。

  • 将您的 Travis 构建限制为仅针对 .NET Core 构建。根据我的经验,这要简单得多。

这是我将在 Noda Time 迁移时使用的 Noda Time .travis.yml 文件 - 至少可以说是初步的,但它确实构建了...

language: csharp
mono: none
dotnet: 1.0.1
dist: trusty

script:
  - dotnet restore src/NodaTime
  - dotnet restore src/NodaTime.Test
  - dotnet restore src/NodaTime.Serialization.Test
  - dotnet build src/NodaTime -f netstandard1.3
  - dotnet build src/NodaTime.Test -f netcoreapp1.0
  - dotnet build src/NodaTime.Serialization.Test -f netcoreapp1.0
  - dotnet run -p src/NodaTime.Test/*.csproj -f netcoreapp1.0 -- --where=cat!=Slow
  - dotnet run -p src/NodaTime.Serialization.Test/*.csproj -f netcoreapp1.0

对此有几点说明:

  • 与早期的 SDK 不同,我们现在需要分别恢复每个项目 - 没什么大不了的 "dotnet restore at the top level" :(
  • 当 运行 在 dist: xenial 上没有出现时,我感到很惊讶,但事实并非如此。 (它声称环境不支持 .NET Core。)我猜这会改变。
  • 我们正在使用 NUnit,目前在新 SDK 中测试的唯一方法是使用 NUnitLite,因此 dotnet run 到 运行 测试
  • 我有点惊讶我不能只为 dotnet run 指定 目录 名称(根据 dotnet restoredotnet build)但这似乎是事情的方式。我会查找错误报告...

无论哪种情况,我都建议 有一个基于 Windows 的 CI 构建来检查所有内容是否在 [=53 上构建和工作=](理想情况下测试您支持的每个框架)。

截至昨天(5 月 5 日),@dasMulli 指出 Mono 发布了与 .NET Core 一起使用的 Mono 5.0 Beta 2 (5.0.0.94)!这里是his post on dotnet/sdk#335. Here is a link to the latest beta release

我的 .travis.yml 文件看起来像:

sudo: required
dist: trusty
language: csharp
solution: MySolution.sln
mono:
  - beta
dotnet: 1.0.3

install:
  - nuget restore MySolution.sln
  - dotnet restore MySolution.sln

script:
  - msbuild /t:Rebuild MySolution.sln

Mono 确实支持构建 VS2017 .Net Framework 项目,因为它现在使用 msbuild。

让它在 Travis CI 上工作有点棘手,但这应该可以解决问题:

  - wget -q https://packages.microsoft.com/config/ubuntu/14.04/packages-microsoft-prod.deb
  - sudo dpkg -i packages-microsoft-prod.deb
  - wget -q http://security.ubuntu.com/ubuntu/pool/main/g/gcc-5/gcc-5-base_5.4.0-6ubuntu1~16.04.9_amd64.deb
  - sudo dpkg -i gcc-5-base_5.4.0-6ubuntu1~16.04.9_amd64.deb
  - wget -q http://security.ubuntu.com/ubuntu/pool/main/g/gcc-5/libstdc++6_5.4.0-6ubuntu1~16.04.9_amd64.deb
  - sudo dpkg -i libstdc++6_5.4.0-6ubuntu1~16.04.9_amd64.deb
  - wget -q http://security.ubuntu.com/ubuntu/pool/main/i/icu/libicu55_55.1-7ubuntu0.4_amd64.deb
  - sudo dpkg -i libicu55_55.1-7ubuntu0.4_amd64.deb
  - sudo apt-get install apt-transport-https
  - sudo apt-get install -y libicu55
  - sudo apt-get install dotnet-runtime-deps-2.1
  - sudo apt-get install dotnet-runtime-2.1
  - sudo apt-get install aspnetcore-runtime-2.1
  - sudo apt-get install dotnet-sdk-2.1

基本上,您需要手动安装 dotnet-sdk 及其所有依赖项。

然后只需调用 msbuild:

  - msbuild /p:Configuration=Release Solution.sln

.NET Framework 目标包 Nuget 包

您现在可以通过添加 NuGet 包在 Linux 上使用 Mono 构建新样式的 csproj 项目:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Label="Build">
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>

  <ItemGroup Label="Package References">
    <PackageReference 
      Include="Microsoft.NETFramework.ReferenceAssemblies" 
      PrivateAssets="All" 
      Version="1.0.0-preview.2" />
  </ItemGroup>

</Project>

可以在 Microsoft/dotnet GitHub 页面上找到更多信息。在撰写本文时它处于预览状态,但我发现它确实有效。我发现的唯一问题是 dotnet test xUnit 项目不起作用,并且根据 xUnit 作者的说法,这不是受支持的方案。