60

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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Joe
  • 741
  • 1
  • 5
  • 6
  • Do you mean you are trying to use net .core references in a System.Windows.Forms project? – Cleptus Oct 24 '19 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 '19 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 '19 at 18:14
  • @bradbury9 , .NET Core is not involved. WinRT is said to be the new WinAPI and I need it to access [Windows.Security.Credentials.PasswordVault](https://learn.microsoft.com/en-us/uwp/api/windows.security.credentials.passwordvault). – Joe Oct 24 '19 at 18:15
  • 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 '19 at 18:21
  • @Joe First reference is in a .net core named folder... `C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll` – Cleptus Oct 24 '19 at 20:21
  • @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 Oct 24 '19 at 21:52
  • 1
    @Jimi thank you so much! Your advise worked. – Joe Oct 24 '19 at 21:55

4 Answers4

138

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.

patrick
  • 16,091
  • 29
  • 100
  • 164
  • 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 '20 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. – Matias Masso Aug 31 '20 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
Chris
  • 141
  • 1
  • 2
1

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

Taran
  • 2,895
  • 25
  • 22
1

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

Lugube
  • 80
  • 6
  • 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 '22 at 15:36