My .NET project targets both .NET 6 and .NET 8, in my csproj file I have:
<PropertyGroup>
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
...
</PropertyGroup>
Then, depending on the target framework I want to specify different dependencies for example:
<!-- Dependency for .NET 6 -->
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Text.Json" Version="6.0.13" />
</ItemGroup>
<!-- Dependency for .NET 8 -->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>
When compiling:
dotnet publish -f net8.0
I get:
Warning NU1603: depends on System.Text.Json (>= 6.0.13) but System.Text.Json 6.0.13 was not found. System.Text.Json 7.0.0 was resolved instead.
The .NET 8 target seems ignored ?
The error your are getting has nothing todo with the publish command, its a build error. Or more precise a dotnet restore error because there is no version 6.0.13 on nuget.org, the highest 6.x version is 6.0.11.
Dotnet restore and build are usually running during a publish.
That being said, you target net6.0 and net8.0 which both already include System.Text.Json as part of the Microsoft.NETCore.App framework.
You can just delete your conditional nuget references for that package, the dotnet runtime will use the "correct" version automatically based on the runtime(s) version you have installed.