Search code examples
.netlinuxmacosdotnet-cli

mac/linux `dotnet build` The reference assemblies for framework ".NETFramework.." were not found


When trying to build Microsoft.NET.Sdk style project that targets net47 rather than .netcore or .net std. The dotnet build will give an error about missing targeting pack. msbuild will work instead, but i'd prefer to use dotnet build. Is there a way.

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

Solution

  • There's a solution that doesn't require depending on MyGet; adding this props import:

       <TargetFramework>net472</TargetFramework>
       <OutDir>bin</OutDir>
     </PropertyGroup>
    +<Import Project="netfx.props" />
     <ItemGroup>
    

    And then include this netfx.props file on your solution:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (c) Microsoft Corporation.  All Rights Reserved.  See License.txt in the project root for license information. -->
    <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <!-- When compiling .NET SDK 2.0 projects targeting .NET 4.x on Mono using 'dotnet build' you -->
        <!-- have to teach MSBuild where the Mono copy of the reference asssemblies is -->
        <TargetIsMono Condition="($(TargetFramework.StartsWith('net4')) OR $(TargetFrameworkVersion.StartsWith('v4.'))) AND '$(OS)' == 'Unix'">true</TargetIsMono>
    
        <!-- Look in the standard install locations -->
        <BaseFrameworkPathOverrideForMono Condition="'$(BaseFrameworkPathOverrideForMono)' == '' AND '$(TargetIsMono)' == 'true' AND EXISTS('/Library/Frameworks/Mono.framework/Versions/Current/lib/mono')">/Library/Frameworks/Mono.framework/Versions/Current/lib/mono</BaseFrameworkPathOverrideForMono>
        <BaseFrameworkPathOverrideForMono Condition="'$(BaseFrameworkPathOverrideForMono)' == '' AND '$(TargetIsMono)' == 'true' AND EXISTS('/usr/lib/mono')">/usr/lib/mono</BaseFrameworkPathOverrideForMono>
        <BaseFrameworkPathOverrideForMono Condition="'$(BaseFrameworkPathOverrideForMono)' == '' AND '$(TargetIsMono)' == 'true' AND EXISTS('/usr/local/lib/mono')">/usr/local/lib/mono</BaseFrameworkPathOverrideForMono>
    
        <!-- If we found Mono reference assemblies, then use them -->
        <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net45' OR '$(TargetFrameworkVersion)' == 'v4.5')">$(BaseFrameworkPathOverrideForMono)/4.5-api</FrameworkPathOverride>
        <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net451' OR '$(TargetFrameworkVersion)' == 'v4.5.1')">$(BaseFrameworkPathOverrideForMono)/4.5.1-api</FrameworkPathOverride>
        <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net452' OR '$(TargetFrameworkVersion)' == 'v4.5.2')">$(BaseFrameworkPathOverrideForMono)/4.5.2-api</FrameworkPathOverride>
        <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net46' OR '$(TargetFrameworkVersion)' == 'v4.6')">$(BaseFrameworkPathOverrideForMono)/4.6-api</FrameworkPathOverride>
        <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net461' OR '$(TargetFrameworkVersion)' == 'v4.6.1')">$(BaseFrameworkPathOverrideForMono)/4.6.1-api</FrameworkPathOverride>
        <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net462' OR '$(TargetFrameworkVersion)' == 'v4.6.2')">$(BaseFrameworkPathOverrideForMono)/4.6.2-api</FrameworkPathOverride>
        <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net47' OR '$(TargetFrameworkVersion)' == 'v4.7')">$(BaseFrameworkPathOverrideForMono)/4.7-api</FrameworkPathOverride>
        <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net471' OR '$(TargetFrameworkVersion)' == 'v4.7.1')">$(BaseFrameworkPathOverrideForMono)/4.7.1-api</FrameworkPathOverride>
        <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net472' OR '$(TargetFrameworkVersion)' == 'v4.7.2')">$(BaseFrameworkPathOverrideForMono)/4.7.2-api</FrameworkPathOverride>
        <EnableFrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != ''">true</EnableFrameworkPathOverride>
    
        <!-- Add the Facades directory.  Not sure how else to do this. Necessary at least for .NET 4.5 -->
        <AssemblySearchPaths Condition="'$(BaseFrameworkPathOverrideForMono)' != ''">$(FrameworkPathOverride)/Facades;$(AssemblySearchPaths)</AssemblySearchPaths>
      </PropertyGroup>
    
      <ItemGroup Condition="'$(TargetFramework)'== 'net472' OR '$(TargetFrameworkVersion)' == 'v4.7.2'">
        <Reference Include="netstandard" />
      </ItemGroup>
    </Project>
    

    (Based on this.)