TExcelTAD
Home › .NET Builds: MSBuild & NuGet › Multi-Targeting: TargetFrameworks

Multi-Targeting: TargetFrameworks

A library used by apps on .NET 8 and .NET 10 (or on .NET Framework 4.8) can be compiled once per target framework from one project. Change TargetFramework to the plural TargetFrameworks, list the frameworks separated by semicolons, and MSBuild runs one build per framework. NuGet packs all of them into a single package, and each consumer gets the best match.

What it takes: targeting packs

To compile for a framework the compiler needs its reference assemblies, which come in a targeting pack. The SDK ships the pack for its own framework (net10.0 here). Packs for other frameworks are downloaded from NuGet on first use: Microsoft.NETCore.App.Ref for net8.0, NETStandard.Library for netstandard2.0, and so on. The machine that records this course has no nuget.org, so we can see exactly that download being attempted:

bash · try it yourself
mkdir multi && cd multi
cat > multi.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net10.0;net8.0</TargetFrameworks>
  </PropertyGroup>
</Project>
XML
echo 'public static class Info { }' > Info.cs
dotnet build -nologo -v:q
ls /usr/share/dotnet/packs
/home/learner/multi/multi.csproj : error NU1101: Unable to find package Microsoft.NETCore.App.Ref. No packages exist with this id in source(s): local
/home/learner/multi/multi.csproj : error NU1101: Unable to find package Microsoft.AspNetCore.App.Ref. No packages exist with this id in source(s): local

Build FAILED.

/home/learner/multi/multi.csproj : error NU1101: Unable to find package Microsoft.NETCore.App.Ref. No packages exist with this id in source(s): local
/home/learner/multi/multi.csproj : error NU1101: Unable to find package Microsoft.AspNetCore.App.Ref. No packages exist with this id in source(s): local
    0 Warning(s)
    2 Error(s)

Time Elapsed 00:00:00.86
Microsoft.AspNetCore.App.Ref
Microsoft.NETCore.App.Host.linux-x64
Microsoft.NETCore.App.Ref

That's the honest state of this sandbox: only net10.0 (plus the ASP.NET Core and app-host packs) is installed, so only net10.0 builds offline. With nuget.org reachable, restore downloads Microsoft.NETCore.App.Ref 8.0.x once into ~/.nuget/packages and the build continues. Corporate CI agents without internet need those packs on their internal feed, which is a classic "works on my machine" failure for multi-targeted libraries.

The rest of this lesson uses a single framework in the plural form: everything MSBuild does for multi-targeting still happens, just with one inner build.

Outer and inner builds

bash · try it yourself
mkdir multi && cd multi
cat > multi.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net10.0</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="System.Memory" Version="4.6.3" />
  </ItemGroup>
  <Target Name="WhoAmI" AfterTargets="Build">
    <Message Importance="high" Text="inner build: TargetFramework='$(TargetFramework)'" />
  </Target>
</Project>
XML
cat > Info.cs <<'CS'
public static class Info
{
#if NET10_0_OR_GREATER
    public const string Runtime = "modern .NET";
#else
    public const string Runtime = "older framework";
#endif
}
CS
dotnet msbuild -getProperty:TargetFrameworks -getProperty:TargetFramework
dotnet msbuild -getProperty:TargetFramework -p:TargetFramework=net10.0
dotnet build -nologo | grep -E 'inner|->'
{
  "Properties": {
    "TargetFrameworks": "net10.0",
    "TargetFramework": ""
  }
}
net10.0
  multi -> /home/learner/multi/bin/Debug/net10.0/multi.dll
  inner build: TargetFramework='net10.0'
  inner build: TargetFramework=''
  • At the project level TargetFramework is empty: this evaluation is the outer build, which only dispatches. For each entry of TargetFrameworks MSBuild evaluates the project again with the global property TargetFramework=<entry>: an inner build. Our WhoAmI target ran inside it and saw net10.0. It also ran a second time with an empty value: the outer build has a Build target of its own (the one that dispatches), and AfterTargets="Build" hooks it too. In a multi-targeted project, guard per-framework hooks with Condition="'$(TargetFramework)' != ''", or the step runs once more than you expect, with half its properties missing.
  • Conditions on $(TargetFramework) therefore work in the project body: the System.Memory reference applies only to a netstandard2.0 inner build (and would need that package in the feed).
  • In code, the SDK defines symbols per framework: NET10_0, NET10_0_OR_GREATER, NETSTANDARD2_0, NETFRAMEWORK… Prefer the _OR_GREATER forms, so that adding net11.0 later doesn't silently fall into the #else branch.
  • dotnet build -f net10.0 (or -p:TargetFramework=…) builds a single inner build, which is handy during development.

Output folders get one subfolder per framework: bin/Debug/net10.0/, bin/Debug/net8.0/. A dotnet pack puts each in lib/<tfm>/ of the package.

Which build does a consumer get?

When a project references a multi-targeted package or project, NuGet picks the nearest compatible framework among those available. A model of that rule for .NET (Core) consumers:

python · try it yourself
# Each framework, the frameworks it can consume, nearest first.
COMPAT = {
    "net10.0": ["net10.0", "net9.0", "net8.0", "net7.0", "net6.0", "net5.0",
                "netcoreapp3.1", "netstandard2.1", "netstandard2.0", "netstandard1.6"],
    "net8.0":  ["net8.0", "net7.0", "net6.0", "net5.0",
                "netcoreapp3.1", "netstandard2.1", "netstandard2.0", "netstandard1.6"],
    "net48":   ["net48", "net472", "net471", "net47", "net462", "net461", "net46",
                "net452", "net451", "net45", "net40", "net35", "net20", "netstandard2.0"],
}

def nearest(consumer, available):
    for tfm in COMPAT[consumer]:
        if tfm in available:
            return tfm
    return None

packages = {
    "Newtonsoft.Json 13.0.4": ["net20", "net35", "net40", "net45", "net6.0", "netstandard1.0", "netstandard1.3", "netstandard2.0"],
    "MyLib (net8.0;netstandard2.0)": ["net8.0", "netstandard2.0"],
    "ModernOnly (net10.0)": ["net10.0"],
}
for name, libs in packages.items():
    for consumer in ["net10.0", "net8.0", "net48"]:
        pick = nearest(consumer, libs)
        print(f"{consumer:8} <- {name:31} lib/{pick}" if pick else f"{consumer:8} <- {name:31} NU1202: not compatible")
net10.0  <- Newtonsoft.Json 13.0.4          lib/net6.0
net8.0   <- Newtonsoft.Json 13.0.4          lib/net6.0
net48    <- Newtonsoft.Json 13.0.4          lib/net45
net10.0  <- MyLib (net8.0;netstandard2.0)   lib/net8.0
net8.0   <- MyLib (net8.0;netstandard2.0)   lib/net8.0
net48    <- MyLib (net8.0;netstandard2.0)   lib/netstandard2.0
net10.0  <- ModernOnly (net10.0)            lib/net10.0
net8.0   <- ModernOnly (net10.0)            NU1202: not compatible
net48    <- ModernOnly (net10.0)            NU1202: not compatible

The model's table is a simplified version of NuGet's real one (which also handles platform-specific frameworks like net10.0-windows, netstandard1.x for .NET Framework, and old portable profiles). It shows the two lessons that matter: a net10.0 app takes Newtonsoft.Json's lib/net6.0 (which is what RuntimeCopyLocalItems showed in the query lesson), and a library that wants .NET Framework consumers must include netstandard2.0 or a net4x target. For .NET Framework consumers NuGet prefers a net4x folder over netstandard2.0, so net48 gets Newtonsoft.Json's lib/net45.

Choosing target frameworks

  • Apps target one framework: the one they run on.
  • Libraries target the oldest framework their consumers need, plus newer ones only when they use newer APIs or gain speed from them. netstandard2.0 remains the widest reach (it covers .NET Framework 4.6.1+ and every .NET Core/.NET).
  • Every extra framework multiplies build time, test time and the number of #if blocks. Drop frameworks that are out of support.

Test yourself

Why did TargetFrameworks net10.0;net8.0 fail to restore in this course's sandbox?

  1. net8.0 is not supported by SDK 10
  2. The net8.0 targeting pack (Microsoft.NETCore.App.Ref) must come from a NuGet feed, and the offline feed doesn't have it
  3. Multi-targeting needs Visual Studio
  4. The project needs an OutputType

In a multi-targeted project, what is $(TargetFramework) during the outer build?

  1. The first framework in the list
  2. Empty: the outer build only dispatches inner builds, each with TargetFramework set as a global property
  3. All frameworks joined with semicolons
  4. net10.0 always

A package ships lib/net6.0 and lib/netstandard2.0. Which does a net10.0 app use?

  1. netstandard2.0
  2. net6.0, the nearest compatible framework
  3. Both
  4. Neither; it needs lib/net10.0