How to solve the error "Must use PackageReference"?

33,636

Solution 1

VS2017/VS2019 -> Solution Explorer -> right-click References -> "Migrate packages.config to PackageReference"

VS2019 -> Solution Explorer -> right-click packages.config -> "Migrate packages.config to PackageReference"

I think this happens when you add a .NET core library to a .NET standard project.

Solution 2

Solution that worked for me was to:

  1. Run a Clean on the project in question
  2. Close Visual Studio
  3. Navigate to the project folder on disk and delete the obj folder
  4. Reopen Visual Studio and Build
Share:
33,636

Related videos on Youtube

Joe
Author by

Joe

Updated on July 09, 2022

Comments

  • Joe
    Joe almost 2 years

    After adding WindowsRuntime to a WinForms-project, building the project fails with Error "Must use PackageReference". The build log is more precise:

    C:[...]\Visual Studio 2017\Projects\Projectname\packages\Microsoft.Windows.SDK.Contracts.10.0.18362.2005\build\Microsoft.Windows.SDK.Contracts.targets(4,5): error : Must use PackageReference

    Here's what I did:

    Install-Package Microsoft.Windows.SDK.Contracts

    with references to:

    • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll,
    • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll
    • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Threading.Tasks.dll
    • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.IO.dll
    • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.InteropServices.WindowsRuntime.dll.

    Is it maybe a thing with the .NET version? In the project's packages.config there is 4.6 while the project targets 4.5.

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
    <package id="Microsoft.Windows.SDK.Contracts" version="10.0.18362.2005" targetFramework="net45"/>
    <package id="System.Runtime.WindowsRuntime" version="4.6.0" targetFramework="net45" />
    <package id="System.Runtime.WindowsRuntime.UI.Xaml" version="4.6.0" targetFramework="net45" />
    </packages>
    

    There are no compile errors... only this strange "PackageReference" error.

    • Cleptus
      Cleptus over 4 years
      Do you mean you are trying to use net .core references in a System.Windows.Forms project?
    • Jimi
      Jimi over 4 years
      Have you set, in Tools -> Options -> Nuget Package Manager -> General -> Package Management to use PackageReference as Package management format? The 4.6 version is related to the System.Runtime.WindowsRuntime dependency, not the .Net Framework.
    • Joe
      Joe over 4 years
      @Jimi, the error occurs when Default package management format is set to Packages.config as well as if it is set to PackageReference. I cleared the NuGet cache and the solution after changing this value. When I installed Microsoft.Windows.SDK.Contracts, the value was Packages.config... did I have to change the value before running Install-Package?
    • Joe
      Joe over 4 years
      @bradbury9 , .NET Core is not involved. WinRT is said to be the new WinAPI and I need it to access Windows.Security.Credentials.PasswordVault.
    • Jimi
      Jimi over 4 years
      Uninstall any NuGet packages related to Microsoft.Windows.SDK.Contracts, set PackageReference as default format, re-install all packages using NuGet Package Manager.
    • Cleptus
      Cleptus over 4 years
      @Joe First reference is in a .net core named folder... C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.W‌​indowsRuntime.dll
    • Joe
      Joe over 4 years
      @bradbury9 you are very mindful! I added the .NET Core library with despair but then figured out that the reference to the .NET4.5 library was sufficient... and somehow forgot to delete the .NET Core reference.
    • Joe
      Joe over 4 years
      @Jimi thank you so much! Your advise worked.
  • schlebe
    schlebe about 4 years
    I have installed using NuGet the new package Microsoft.Web.WebView2 and I have also installed Microsoft Edge Chromium. After that, I have rebuild application and I have receive “Must use PackageReference" error ! I have done Patrick proposal and build now work as before. Thanks
  • Matias Masso
    Matias Masso over 3 years
    I was missing migration option in References context menu from VS2019, but it can also be found at packages.config context menu.
  • Adam McKee
    Adam McKee about 2 years
    There is a nuget package cache in the obj folder, which must be removed to clear that error message.
  • Ruben
    Ruben about 2 years
    Yes that is, this error happened to mi after install Microsoft.Windows.SDK.Contracts and I had to remove all reference in the file you mention