Simplest way to build dotnet SDK project requiring net461 on MacOS

(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.


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)