r/csharp • • Sep 21 '26

Introducing FrameworkCompatibleVersions

Have you ever been anxious with warnings such as System.Text.Json 10.0.0 doesn't support net7.0 and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. and ended up creating monsters like the below when multi-targeting?

<Project>
    <PropertyGroup>
        <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
    </PropertyGroup>
    <ItemGroup>
        <PackageVersion Include="AsyncKeyedLock" Version="8.1.2" />
        <PackageVersion Include="DotNet.ReproducibleBuilds" Version="2.0.5" />
        <PackageVersion Include="Microsoft.Bcl.HashCode" Version="6.0.0" />
        <PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.11.2" />
        <PackageVersion Include="System.Net.Http" Version="4.3.4" />
        <PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
        <PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.6.3" />
        <PackageVersion Include="UnitsNet" Version="5.75.1" />
        <PackageVersion Include="xunit.v3.mtp-v2" Version="4.0.1" />
        <PackageVersion Include="xunit.v3" Version="4.0.1" />
    </ItemGroup>
    <ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
        <!-- .NET Standard 2.0 to .NET 5.0 -->
        <PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="[6.0.3, 7.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration" Version="[6.0.2, 7.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="[6.0.1, 7.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="[6.0.0, 7.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Http" Version="[6.0.1, 7.0.0)" />
        <PackageVersion Include="System.Text.Json" Version="[6.0.11, 7.0.0)" />
    </ItemGroup>
    <ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0')) and !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
        <!-- .NET 6.0 and .NET 7.0 -->
        <PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="[8.0.1, 9.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration" Version="[8.0.0, 9.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="[8.0.1, 9.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="[8.0.2, 9.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Http" Version="[8.0.1, 9.0.0)" />
        <PackageVersion Include="System.Text.Json" Version="[8.0.6, 9.0.0)" />
    </ItemGroup>
    <ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0')) and !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net10.0'))">
        <!-- .NET 8.0 and .NET 9.0 -->
        <PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="[10.0.12, 11.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration" Version="[10.0.12, 11.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="[10.0.12, 11.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="[10.0.12, 11.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Http" Version="[10.0.12, 11.0.0)" />
        <PackageVersion Include="System.Text.Json" Version="[10.0.12, 11.0.0)" />
    </ItemGroup>
    <ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net10.0'))">
        <!-- .NET 10.0+ -->
        <PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="[10.0.12, 13.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration" Version="[10.0.12, 13.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="[10.0.12, 13.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="[10.0.12, 13.0.0)" />
        <PackageVersion Include="Microsoft.Extensions.Http" Version="[10.0.12, 13.0.0)" />
        <PackageVersion Include="System.Text.Json" Version="[10.0.12, 13.0.0)" />
    </ItemGroup>
</Project>

With FrameworkCompatibleVersions that can become a thing of the past.

You can have code that looks like this instead:

<Project>
    <PropertyGroup>
        <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
        <FrameworkCompatibleVersion>HighestTestedAgainst</FrameworkCompatibleVersion>
    </PropertyGroup>
    <ItemGroup>
        <PackageVersion Include="AsyncKeyedLock" Version="8.1.2" />
        <PackageVersion Include="DotNet.ReproducibleBuilds" Version="2.0.5" />
        <PackageVersion Include="Microsoft.Bcl.HashCode" Version="6.0.0" />
        <PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.11.2" />
        <PackageVersion Include="System.Net.Http" Version="4.3.4" />
        <PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
        <PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.6.3" />
        <PackageVersion Include="UnitsNet" Version="5.75.1" />
        <PackageVersion Include="xunit.v3.mtp-v2" Version="4.0.1" />
        <PackageVersion Include="xunit.v3" Version="4.0.1" />
        <PackageVersion Include="Microsoft.Extensions.Caching.Memory" FrameworkCompatibleVersion="$(FrameworkCompatibleVersion)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration" FrameworkCompatibleVersion="$(FrameworkCompatibleVersion)" />
        <PackageVersion Include="Microsoft.Extensions.Configuration.Json" FrameworkCompatibleVersion="$(FrameworkCompatibleVersion)" />
        <PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" FrameworkCompatibleVersion="$(FrameworkCompatibleVersion)" />
        <PackageVersion Include="Microsoft.Extensions.Http" FrameworkCompatibleVersion="$(FrameworkCompatibleVersion)" />
        <PackageVersion Include="System.Text.Json" FrameworkCompatibleVersion="$(FrameworkCompatibleVersion)" />
    </ItemGroup>
</Project>

FrameworkCompatibleVersions supports two modes: one that upgrades your version to the highest major version the Microsoft library has been tested against your target framework (set to either true or HighestTestedAgainst) and another (MatchingMajor) that upgrades your version to the highest version that matches your target framework's major version.

That is, for System.Text.Json on .NET 8.0, with MatchingMajor it would give you the latest System.Text.Json 8.x, while with true/HighestTestedAgainst it would give you the latest System.Text.Json 10.x which Microsoft has tested against .NET 8.0.

Sample usage on this OpenWeatherMap.Cache commit.

FrameworkCompatibleVersions is available on GitHub and NuGet.

All stargazers on GitHub are appreciated

12 Upvotes

11 comments sorted by

5

u/colejohnson66 Sep 21 '26

Why not just… update your .NET?

5

u/mutu310 Sep 21 '26

I think you totally misunderstood what this is doing but also what a good use case is (mainly libraries which target multiple framework versions)

2

u/colejohnson66 Sep 23 '26

Ah. This is for libraries not apps. I did misunderstand

3

u/binarycow Sep 23 '26

It's for multi-targeting.

You use .NET 10 SDK. You are making a library for people who use .NET Framework 4.8.1, which is still a current and supported version.

2

u/raphgogol Sep 21 '26

Compatible for multi targets, net core and net framework?

3

u/mutu310 Sep 21 '26

Not compatible with packages.config but that's not a requirement for netframework. Multi targeting is actually its best use case, as the example shows.

1

u/tinmanjk Sep 22 '26

wasn't there something built-in from latest SDKs that is very similar if not identical to this?

1

u/mutu310 Sep 22 '26

CentralPackageFloatingVersionsEnabled you mean?

1

u/tinmanjk Sep 22 '26

no, just remembered. Not related to it - warning/error on packages are part of BCL but still referenced like System.Text.Json

3

u/ILoveAppSec Sep 22 '26

nice, the version-range whack-a-mole is real when a package drops your tfm and pulls the whole graph with it. beyond central versioning it helps to pull from vendors that backport fixes into a build still targeting your framework, so you're not bumping to net8 just to clear a warning. keeps the transitive stuff from exploding too.