Search code examples
c#f#visual-studio-2017portable-class-librarymsbuild-15

.NET Multi-Target with .NET Standard and Portable Library MSBuild 15


Porting to .NET standard version, you may be source identical to a portable library, but the platforms supported may not.

For example .NET standard 1.6 might be the lowest version that has your api's available from portable Profile47. Profile47 supports .net 4.5 or later, nut .NET Standard 1.6 only supports 4.6.1 and later!

With the new multi-targeting with the new msbuild 15 csproj/fsproj (also VS2017) is it possible to compile for both a Portable Library and .Net Standard at the same time to make the transition easier?


Solution

  • The prior answer is accurate but it's not actually enough. You need to reference the Portable language targets to get the correct behavior. There also may be other SDK references and you'll probably want implicitly defined ifdefs like PROFILE328.

    I wrote a NuGet package you can add to your project that "does the right thing" here:

    https://github.com/onovotny/MSBuildSdkExtras

    Edit: csproj would look like this:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFrameworks>netstandard1.6;portable-net45+sl5+win8</TargetFrameworks>
        <Description>YOUR DESCRIPTION</Description>
        <Company>YOUR COMPANY</Company>
        <Authors> YOUR AUTHORS </Authors>
        <Copyright>YOUR COPYRIGHT</Copyright>
        <AssemblyVersion>YOUR VERSION</AssemblyVersion>
        <FileVersion>YOUR VERSION</FileVersion>
        <PackageProjectUrl>YOUR PROJECT URL</PackageProjectUrl>
        <PackageLicenseUrl>YOUR LICENSE</PackageLicenseUrl>
        <PackageTags>YOUR TAGS</PackageTags>
        <IncludeSymbols>True</IncludeSymbols>
        <IncludeSource>True</IncludeSource>
        <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
        <PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
        <Version>YOUR NUGET VERSION</Version>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="MSBuild.Sdk.Extras" Version="1.0.5" PrivateAssets="all" />
      </ItemGroup>
    
      <Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
    </Project>