Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to build dotnet SDK project requiring net461 on MacOS

I have a dotnet SDK .sln (and a build.proj) with <TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>. It builds on Windows using Visual Studio and dotnet build, but I'd also like it to build as many other places as possible. What do I need to put in my README.md and/or what can I put in the project files to make it build on Rider and/or on from bash on a Mac?

like image 672
Ruben Bartelink Avatar asked Aug 06 '19 18:08

Ruben Bartelink


Video Answer


2 Answers

(using .NET Core SDK) The simplest way to build for a .NET Framework TFM when running on either macOS or Linux using the .NET Core CLI, is to utilize the .NET Framework Targeting Pack Nuget Packages from Microsoft (currently in preview):

These packages enable building .NET Framework projects on any machine with at least MSBuild or the .NET Core SDK installed.

The following scenarios and benefits are enabled for .NET Framework projects:

  • Build without requiring admin operations to install pre-requisites such as Visual Studio or .NET Framework targeting packs.
  • Build libraries on any operating system supported by the .NET Core SDK.
  • Build Mono-based projects.

You may either include the Microsoft.NETFramework.ReferenceAssemblies metapackage; or use just the specific package, which is in your case Microsoft.NETFramework.ReferenceAssemblies.net461.

Add the package to the *.csproj or your Directory.Build.props:

<Project>
  <ItemGroup>
    <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
  </ItemGroup>
</Project>

Note: The PrivateAssets attribute controls which dependency assets will be consumed but won't flow to the parent project. See the docs.

Update

This is no longer required using the .NET 5 SDK (e.g. 5.0.100), which will now automatically add the PackageReference to the ReferenceAssemblies for .NET Framework.

like image 141
FlashOver Avatar answered Sep 27 '22 22:09

FlashOver


In order to build via bash on a vanilla Mac, the minimal steps seem to be:

  • Install Mono 6.0 (5.2 is recommended for VS Mac interop, I dont care about that, and Mono 6.0's interop with Dotnet core is better)
  • Install dotnet SDK 2.2 (doesn't have to be exactly that, but it works for me)
  • Put this in a Directory.build.props file (open to improvements if anyone has any)
<Project>
     <PropertyGroup>
       <IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))'== 'true'">true</IsOSX>
     </PropertyGroup>
     <PropertyGroup Condition=" '$(IsOSX)' == 'true' ">
        <FrameworkPathOverride>/Library/Frameworks/Mono.framework/Versions/Current/Commands/../lib/mono/4.6.1-api</FrameworkPathOverride>
     </PropertyGroup>
</Project>
  • Bash: dotnet build SolutionFileName.sln should now work
  • Install Rider 2019.1 or later
  • Rider: should Just Work (it should autodetect msbuild 16.0 in the build tools section)
like image 34
Ruben Bartelink Avatar answered Sep 27 '22 23:09

Ruben Bartelink