Search code examples
c#.net-corecsproj

How to target multiple frameworks for a library


I am making this Standard Library to target according to the downloaded framework. For example for .NET Core it will download the packages that are for .NET Core

 <!--Trageting .NET CORE (2.1, 3.0) && .NET STANDARD (2.0,2.1) -->
  <ItemGroup Condition="$(TargetFramework.StartsWith('netcore')) OR $(TargetFramework.StartsWith('netstandard')) ">
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
    <!--<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.7" />-->
    <PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" />
    <PackageReference Include="NLog.WindowsEventLog" Version="4.6.7" />
    <PackageReference Include="System.Diagnostics.EventLog" Version="4.6.0" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.6.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.7.0" />
    <PackageReference Include="System.Linq" Version="4.3.0" />
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.6.0" />
  </ItemGroup>
  
  <!--Targeting .NET FRAMEWORK (4.6.1 4.6.2)  -->
  <ItemGroup Condition="'$(TargetFramework)' == 'net461' OR '$(TargetFramework)' == 'net462'">
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Client" version="5.2.3" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Core" version="5.2.3" />
    <PackageReference Include="NLog.Extensions.Logging" Version="1.5.4" />
    <PackageReference Include="NLog.WindowsEventLog" Version="4.6.7" />
    <PackageReference Include="System.Diagnostics.EventLog" Version="4.5.0" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
    <PackageReference Include="System.Linq" Version="4.3.0" />
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
  </ItemGroup>

   <!--Targeting .NET FRAMEWORK (4.7, 4.7.1, 4.7.2)-->
  <ItemGroup Condition="'$(TargetFramework)' == 'net47' &#xD;&#xA; OR '$(TargetFramework)' == 'net471'&#xD;&#xA; OR '$(TargetFramework)' == 'net472'">
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Client" version="5.2.3" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Core" version="5.2.3" />
    <PackageReference Include="NLog.Extensions.Logging" Version="1.5.4" />
    <PackageReference Include="NLog.WindowsEventLog" Version="4.6.7" />
    <PackageReference Include="System.Diagnostics.EventLog" Version="4.5.0" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
    <PackageReference Include="System.Linq" Version="4.3.0" />
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
  </ItemGroup>

Is there any better way of doing this above code as mentioned like using DefineConstants?


Solution

  • If the thing you want to change is the versions with them having some amount of commonality, then I might be tempted to do something like:

    <PropertyGroup>
        <VersionWebApiClient>5.2.3</VersionWebApiClient>
        <VersionWebApiClient Condition="...todo...">5.2.7</VersionWebApiClient>
        <!-- ... for the other things ... -->
    <PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="$(VersionWebApiClient)" />
        <!-- ... for the other things ... -->
    </ItemGroup>
    

    This:

    1. allows the most common values to be simple defaults
    2. allows clear overrides per target framework
    3. doesn't duplicate the actual package references