0

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 ?

4
  • No, it does not look like it was ignored. It's just everything was fine with v.8. Did you get build output for both .NET 6 and .NET 8? Commented Aug 22 at 13:01
  • why it talks about .net 6 when asking to compile for .net 8 ? I just have .net 8 output Commented Aug 22 at 14:23
  • No, this is you who has requested building it for .NET 6, because that was you who specified <TargetFrameworks>net8.0;net6.0</TargetFrameworks>. Please ask yourself why you do so. You have mentioned “My .NET project targets...” in your question. Do I miss something? Commented Aug 22 at 14:44
  • dotnet publish -f net8.0 ; this is the command to publish (and build , right ?) for net8.0 explicitely ; at least this is what I thought is right... Indeed my code is compatible with net 6 and 8 provided it has the right dependencies, hence the question Commented Aug 24 at 22:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.