Search code examples
c#csproj

How to use Microsoft.Build.CentralPackageVersions?


I try to use the Central Package Version from don't.

My .csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <Sdk Name="Microsoft.Build.CentralPackageVersions" Version="2.0.1" />

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" />
  </ItemGroup>
</Project>

My Package.props looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <PackageReference Update="Newtonsoft.Json"          Version="12.0.3" />
  </ItemGroup>
</Project>

But that results in a warning with a false version of Newtonsoft.Json

Solution Explorer

Package 'Newtonsoft.Json 7.0.1' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project. AppInsightsClient   C:\Users\peer.nissen\source\repos\AppInsightsClient\AppInsightsClient\AppInsightsClient.csproj  1   

My file structure looks like that:

Solution
|-Package.props
|-Solution.sln
|-Project
   |-Project.csproj

Can you help me, please?


Solution

  • Your Packages.props should like this:

    <?xml version="1.0" encoding="utf-8"?>
    
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
    <ItemGroup>
    <PackageReference Update="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Update="Microsoft.Extensions.Configuration" Version="5.0.0" />
    <PackageReference Update="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
    </ItemGroup>
    
    </Project>
    

    And for you .csproj, it should like this:

    <Project Sdk="Microsoft.NET.Sdk">
    
    <Sdk Name="Microsoft.Build.CentralPackageVersions" Version="2.0.1" />
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <RuntimeIdentifiers>linux-x64;win-x64</RuntimeIdentifiers>
        <AssemblyName>HelloWorld</AssemblyName>
        <RootNamespace>HelloWorld</RootNamespace>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Newtonsoft.Json" />
        <PackageReference Include="Microsoft.Extensions.Configuration"/>
        <PackageReference Include="Microsoft.Extensions.Configuration.Json" />
      </ItemGroup>
    
    </Project>