How to solve the error "Must use PackageReference"?
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:
- Run a Clean on the project in question
- Close Visual Studio
- Navigate to the project folder on disk and delete the obj folder
- Reopen Visual Studio and Build
Related videos on Youtube
Joe
Updated on July 09, 2022Comments
-
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 over 4 yearsDo you mean you are trying to use net .core references in a System.Windows.Forms project?
-
Jimi over 4 yearsHave you set, in
Tools -> Options -> Nuget Package Manager -> General -> Package Management
to usePackageReference
as Package management format? The 4.6 version is related to theSystem.Runtime.WindowsRuntime
dependency, not the .Net Framework. -
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 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 over 4 yearsUninstall any NuGet packages related to
Microsoft.Windows.SDK.Contracts
, setPackageReference
as default format, re-install all packages using NuGet Package Manager. -
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.WindowsRuntime.dll
-
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 over 4 years@Jimi thank you so much! Your advise worked.
-
schlebe about 4 yearsI 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 over 3 yearsI was missing migration option in References context menu from VS2019, but it can also be found at packages.config context menu.
-
Adam McKee about 2 yearsThere is a nuget package cache in the
obj
folder, which must be removed to clear that error message. -
Ruben about 2 yearsYes 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