61

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.

8
  • Do you mean you are trying to use net .core references in a System.Windows.Forms project?
    – Cleptus
    Oct 24, 2019 at 11:28
  • 5
    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.
    – Jimi
    Oct 24, 2019 at 11:29
  • @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
    Oct 24, 2019 at 18:14
  • 8
    Uninstall any NuGet packages related to Microsoft.Windows.SDK.Contracts, set PackageReference as default format, re-install all packages using NuGet Package Manager.
    – Jimi
    Oct 24, 2019 at 18:21
  • 1
    @Jimi thank you so much! Your advise worked.
    – Joe
    Oct 24, 2019 at 21:55

4 Answers 4

140

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.

2
  • 3
    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
    – schlebe
    Apr 18, 2020 at 14:38
  • 5
    I was missing migration option in References context menu from VS2019, but it can also be found at packages.config context menu. Aug 31, 2020 at 11:26
14

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
1
  • 1
    There is a nuget package cache in the obj folder, which must be removed to clear that error message. Feb 25, 2022 at 11:20
1

Could be becuse of nugetpackage, for WPF use WPF one.

1

Go to your .csproj and remove the references to the package. On my case they were at the end of the XML file

1
  • 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
    – Ruben
    Apr 13, 2022 at 15:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.