Switch back to packages.config - nuget

Is it possible to switch back to packages.config scheme of things? I moved to PackageReference scheme when I created the project, but for some reasons need to move to old way of things now.
I have tried Package Manager Console commands like restore with Update and Reinstall options. They do not generate packages.config anymore. I also switched Tools > Options > NuGet Package Manager to packages.config. No good.

You'll need to:
Add an "empty" packages.config file to the project (that is, xml as in the example but without individual <package> elements)
Unload the project or close vs and open the csproj file in a text editor.
Remove all PackageReference elements out of the project file.
Make sure that <RestoreProjectStyle> is not set in the project file.
Reopen the project in VS.

There was one step missing for me:
4.5. Make sure that Tools>Options>NuGet Package Manager>General>Default package management format is set to Packages.config

Wanted to add one more thing. I tried all above steps and Visual Studio kept trying to use the global-packages location, instead of repositoryPath from Nuget.config. Finally found that there was a package-lock.json file in the obj dir of my projects that had the global package dir stored in it. For some reason the Clean operation, in Visual Studio, didn't delete the file, so I wound up doing a
git clean -x -d -f
At the root of my repository. Which deletes all un-tracked files and directores, and ignores the patterns the .gitignore file. It was a bit excessive, I could have tried to find them all by hand, but it got the job done. Now Nuget restore properly uses the repositoryPath setting specified in the Nuget.Config file.

How I made it work:
Take of screenshot of the packages we use in this project
Add an "empty" packages.config file to the project and copy this into it:
<?xml version="1.0" encoding="utf-8"?>
<packages>
</packages>
Unload the project and open the csproj file in a text editor.
Remove all PackageReference elements out of the project file.
Make sure that <RestoreProjectStyle> is not set in the project file.
Make sure that:
Tools >
Options>
NuGet Package Manager >
General>
Default package management format is set to Packages.config
Delete the obj folder in the project
Use the screenshot to re-download the packages again

Related

Change nuget package location in Linqpad

I am trying to install the Active Directory Authentication Library in Linqpad Nuget manager and I am getting the path/location too long message:
I have attempted to change the install location by adding a setting for repository path to the LinqPad.nuget.config, which now looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="C:\Ng" />
</config>
</configuration>
However, the packages are still being installed in the user/appdata folder.
How can I fix this?
You can now, from version v5.06.05 (in beta at time of writing).
Go to LINQPad's NuGet package manager and click Settings, then Advanced.
In 2021 and with Linqpad 6 you can change the path for local packages by adding a system variable "NUGET_PACKAGES" with value set to whatever path you like.
I had the same issue with my LINQPad 5 (v5.44.02).
All the Nuget packages you download from LINQPad will be saved/stored in some folder on your computer. By default, these packages will be saved in the %LocalAppData%\LINQPad\NuGet.FW46 folder.
NuGet Settings -> Advanced
If you happen to have a long username then sometimes the file path of the package you are going to download will exceed the maximum limit.
I was able to fix this issue by updating this folder to a location something like "E:LP_Nuget". This actually reduced the file path length, and I was succesfully able to download the packages. With this change, all your LINQPad NuGet package will now be downloaded to this new folder.
Open LINQPad NuGet Manager
Ctrl + Shift + P
Click on the "Settings" button at the bottom left corner.
Select the "Advanced" tab and you should be able to see/change the NuGet package folder location.
Update this to a folder which has short file path. For instance, something like "C:\LINQPad_Packages".

Use Contents of Packages.config in One Project to Load Nuget Packages in a New Project

I have a situation whereby I want to create a new project (in a new solution) which will have the exact same Nuget packages as an existing project.
I would like to find a way, if possible, to leverage the Packages.config file in my existing project to download those packages into the new solution. The idea being, to save me from having to run Install Package etc. for each and every package that I want to install.
Note, I am not talking about cloning a repository. Nuget package restore works beautifully in that situation. However, if I copy the Packages.config contents into the new solution's Packages.config, Nuget package restore does not bring down the packages listed in that config file.
So, is there any command or some such that I can run which will look at the config file and just go ahead and load those packages?
The packages.config file specifies the NuGet packages that have already been installed into the project. If you use NuGet.exe restore solution.sln or NuGet.exe install packages.config then all that will happen is that the .nupkg files will be downloaded. Your project will not be modified so if it is missing references to assemblies in the NuGet package it will still be missing them afterwards.
However what you can do with Visual Studio is:
Open the project with the packages.config file in it
Open the Package Manager Console
Click the Restore button that is displayed at the top of the Package Manager Console window to download the files into the packages directory.
Run the command update-package -reinstall in the Package Manager Console window.
Step 4. will cause Visual Studio and NuGet to add the references back to your project. The Update-Package -reinstall command uninstalls and then installs the NuGet packages back into the project keeping the versions as specified in the packages.config file.

How to enable nugets package restore in Visual Studio 2015?

How do I enable the nuget package restore feature in Visual Studio 2015?
The feature is just missing in my context menu (Yes - I've double checked that I try it on the solution instead of a project)
My only project is a .NET 4.5 console application - there is no .nuget folder yet. There is an actual nuget package installed.
I had the same problem as you and the way I solved it was to delete the packages folder from my solution and also bin and obj folders from every project in the solution and give it a rebuild.
As it turns out, the real solution is to migrate to Automatic Package Restore. We experienced issues with NuGet after upgrading to VS 2015 after working in 2013 for a while. Turns out the old way of doing NuGet completely hoses the new way of doing it.
The solution is simple, though tedious. Apparently the NuGet.targets file signals VS to use the old NuGet way of doing things, and it's absence means that you are now using "Automatic Package Restore". You can migrate to Automatic Package Restore by following these steps:
Delete .nuget/NuGet.exe
Delete .nuget/NuGet.targets
For each project:
Unload the project
Right click -> Edit the project
Delete all references to the NuGet.targets file, i.e. the following:
<RestorePackages>true</RestorePackages>
...
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
...
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
There is also a powershell script that will perform the migration for you, if you are feeling bold. You can find it on github.
For others this option is available in the Nuget settings section in Visual Studio Options
I had problem with restoring package with error 401 unauthorised. I found the solution by unchecking available local package sources.
Debug > Options > NuGet Package Manager > Package Sources
Only allow nuget.org packages
My Visual Studio 2015 Professional was installed using the default settings. The options for "Allow NuGet to download missing packages" and "Automatically check for missing packages during build in Visual Studio" were already checked by default, but I still could not download the packages. I was getting an error that a package version could not be found.
I needed to click a checkbox under Tools > Options > NuGet Package Manager > Package Sources to enable NuGet Package Restore to work as expected.
I had a problem with accessing the packages folder after starting Visual Studio 2015 in Administrator mode, so I deleted it with the intention of performing a restore.
But no restore was performed when rebuilding the solution, and there was no Restore Packages to be found at the solution level or in the Nuget window.
However, if you right-click the References nodes on the projects in the solution, there is a Restore Packages option. Running this on all projects fixed it for me.
enter image description here
Well it is pretty simple with VS2015, all i had to do was to right click on my project solution and click on restore nuget.

NuGet Package Restore Not Working

I checked in a project on one computer, checked out on another, and find that the binaries installed by NuGet are missing. I could check them in to source control as well, but it looks like there's a better solution:
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
I followed those instructions, now have a .nuget folder where one should be, have the following entries in my .csproj file:
<RestorePackages>true</RestorePackages>
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
and yet when I rebuild my solution, the missing packages are not restored.
What am I missing? How can I diagnose this problem?
Note you can force package restore to execute by running the following commands in the nuget package manager console
Update-Package -Reinstall
Forces re-installation of everything in the solution.
Update-Package -Reinstall -ProjectName myProj
Forces re-installation of everything in the myProj project.
Note: This is the nuclear option. When using this command you may not get the same versions of the packages you have installed and that could be lead to issues. This is less likely to occur at a project level as opposed to the solution level.
You can use the -safe commandline parameter option to constrain upgrades to newer versions with the same Major and Minor version component. This option was added later and resolves some of the issues mentioned in the comments.
Update-Package -Reinstall -Safe
For others who stumble onto this post, read this.
NuGet 2.7+ introduced us to Automatic Package Restore. This is considered to be a much better approach for most applications as it does not tamper with the MSBuild process. Less headaches.
Some links to get you started:
The right way to restore NuGet packages
Migrate away from MSBuild-based NuGet package restore
Migrating MSBuild-Integrated solutions to use Automatic Package Restore
You have to choose one way of the following :
Re-installing a package by it's name in all solution's projects:
Update-Package –reinstall <packageName>
Re-installing a package by it's name and ignoring it's dependencies in all solution's projects:
Update-Package –reinstall <packageName> -ignoreDependencies
Re-installing a package by it's name in a project:
Update-Package –reinstall <packageName> <projectName>
Re-installing all packages in a specific project:
Update-Package -reinstall -ProjectName <projectName>
Re-installing all packages in a solution:
Update-Package -reinstall
Did you enable package restore mode in the project that has the missing packages/binaries ? There's a known issue that requires the packages to be correctly installed when enabling the restore mode :
http://nuget.codeplex.com/workitem/1879
Original link is dead; this might be a replacement: https://github.com/NuGet/Home/issues/1968
VS 2017
Tools>NuGet Package Manager>Package Manager Settings>General
Click on "Clear All NuGet Cache(s)"
I have run into this problem in two scenarios.
First, when I attempt to build my solution from the command line using msbuild.exe.
Secondly, when I attempt to build the sln and the containing projects on my build server using TFS and CI.
I get errors claiming that references are missing. When inspecting both my local build directory and the TFS server's I see that the /packages folder is not created, and the nuget packages are not copied over. Following the instructions listed in Alexandre's answer http://nuget.codeplex.com/workitem/1879 also did not work for me.
I've enabled Restore Packages via VS2010 and I have seen builds only work from within VS2010. Again, using msbuild fails.My workaround is probably totally invalid, but for my environment this got everything working from a command line build locally, as well as from a CI build in TFS.
I went into .\nuget and changed this line in the .nuget\NuGet.targets file:
from:
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" -o "$(PackagesDir)"</RestoreCommand>
to: (notice, without the quotes around the variables)
<RestoreCommand>$(NuGetCommand) install $(PackagesConfig) -source $(PackageSources) -o $(PackagesDir)</RestoreCommand>
I understand that if my directories have spaces in them, this will fail, but I don't have spaces in my directories and so this workaround got my builds to complete successfully...for the time being.
I will say that turning on diagnostic level logging in your build will help show what commands are being executed by msbuild. This is what led me to hacking the targets file temporarily.
If anything else didn't work, try:
Close Project.
Delete packages folder in your solution folder.
Open Project again and restore Nugget Packages again.
Worked for me and it's easy to try.
If none of the other answers work for you then try the following which was the only thing that worked for me:
Find your .csproj file and edit it in a text editor.
Find the <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> tag in your .csproj file and delete the whole block.
Re-install all packages in the solution:
Update-Package -reinstall
After this your nuget packages should be restored, i think this might be a fringe case that only occurs when you move your project to a different location.
Just for others that might run into this problem, I was able to resolve the issue by closing Visual Studio and reopening the project. When the project was loaded the packages were restored during the initialization phase.
For me, I had an empty tag NuGetPackageImportStamp in .csproj
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
It should ideally contain some valid GUID.
Removing this tag and then "Restore Nugets" worked for me.
In VS2017, right-click on the solution => Open CommandLine => Developer Command Line.
Once thats open, type in (and press enter after)
dotnet restore
That will restore any/all packages, and you get a nice console output of whats been done...
None of the other solutions worked in my situation:
AspNetCore dependencies had been installed/uninstalled and were being cached. 'AspNetCore.All' would refuse to properly update/reinstall/remove. And regardless of what i did, it would use the cached dependencies (that it was not compatible with), because they were a higher version.
Backup Everything. Note the list of Dependencies you'll need to reinstall, Exit VisualStudio
Open up all .proj files in a text editor and remove all PackageReference
In each project, delete the bin, obj folders
Delete any "packages" folders you find in the solution.
Open solution, go into Tools > Nuget Package Manager > Package Manager
Settings and Clear all Nuget caches. Check the console because it may
fail to remove some items - copy the folder path and exit visual
studio.
Delete anything from that folder Reopen solution and start
installing nuget packages again from scratch.
If that still doesn't work, repeat but also search your drive in windows explorer for nuget and delete anything cachey looking.
Sometimes something strange happens and using Visual Studio to automatically restore doesn't work. In that case you can use the NuGet Package Manager Console. That is opened within Visual Studio from Tools -> NuGet Package Manager -> Package Manager Console. The commands within the console are simple. And to get context help while typing a command just press the button and it will give you all options that start with the letters you're typing. So if a package isn't installed, for example log4net, type the following command:
Install-Package log4net
You can do a whole lot more, like specify the version to install, update a package, uninstall a package, etc.
I had to use the console to help me when Visual Studio was acting like a weirdo.
Automatic Package Restore will fail for any of the following reasons:
You did not remove the NuGet.exe and NuGet.targets files from the solution's .nuget folder (which can be found in your solution root folder)
You did not enable automatic package restore from the Tools >> Options >> Nuget Package Manager >> General settings.
You forgot to manually remove references in all your projects to the Nuget.targets file
You need to restart Visual Studio (make sure the process is killed from your task manager before starting up again).
The following article outlines in more detail how to go about points 1-3:
https://docs.nuget.org/consume/package-restore/migrating-to-automatic-package-restore
I had NuGet packages breaking after I did a System Restore on my system, backing it up about two days. (The NuGet packages had been installed in the meantime.) To fix it, I had to go to the .nuget\packages folder in my user profile, find the packages, and delete them. Only then would Visual Studio pull the packages down fresh and properly add them as references.
The best workaround that I found creating a new Project from scratch,
then import all the source files with the code.
My project was not so complicated so I had no problem from there.
I experienced this problem recently as well. The usual 'dotnet restore' and 'Update-Package -reinstall' didn't help.
I looked at the .csproj file and found that the values for these packages were inconsistent. Some were pointing to <solutiondir>\packages and some were <projectdir>\packages.
I made them all point to the same place (I believe that was to <projectdir>\packages) and then I was able to restore the NuGet packages.
I'm not sure how the .csproj file ended up pointing to two different places. Maybe from starting out in Visual Studio 2017 (or earlier) and then moving to Visual Studio 2019? Or different settings used by different programmers? I really don't know.
vs2015 no enable nuget restore problem.
My solution:
add folder .nuget, add file NuGet.Config and NuGet.targets in Directory .nuget
each project file add:
build
<RestorePackages>true</RestorePackages>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
If the error you are facing is "unable to connect to remote server" as was mine, then it would benefit you to have this check as well in addition to the checks provided in the above comments.
I saw that there were 2 NUGET Package Sources from which the packages could be downloaded (within Tools->Nuget Package Manager->Packager Manager Settings). One of the Package Source's was not functioning and Nuget was trying to download from that source only.
Things fell into place once I changed the package source to download from: https://www.nuget.org/api/v2/ EXPLICTLY in the settings
In my case, an aborted Nuget restore-attempt had corrupted one of the packages.configfiles in the solution. I did not discover this before checking my git working tree. After reverting the changes in the file, Nuget restore was working again.
There is a shortcut to make Nuget restore work.
Make sure internet connection or Nuget urls are proper in VS Tools options menu
Look at .nuget or nuget folder in the solution, else - copy from any to get nuget.exe
DELETE packages folders, if exists
Open the Package manager console execute this command
paste full path of nuget.exe RESTORE full path of .sln file!
use Install-pacakge command, if build did not get through for any missing references.
The same problem I encountered but what solved my problem was to go to Nuget Package Manager (rightclick to the prj > select Manage Nugget Packages) and uninstall and install again the packages that are having issues.
BUT there are some packages having dependencies to other packages, so what I did was to uninstall the dependencies first and then followed by the nuget package that I wanted to uninstall and re-install.
You can find out if the nuget has dependencies when you tried to uninstall it and then visit the Output window, there will be a line saying that the nuget failed to uninstall because of its dependencies, and those dependencies are also listed there.
I know this is an old thread but it was the first search result today (in 2022) for "nuget package restore canceled". This is the message I got in VS when trying to rebuild a solution at my new job.
I've been in the new job for a few weeks and the build worked up to now.
The package source used by my solution is xxxxPackages, where xxxx is the name of my employer. So the package source is not nuget.org, which I normally use.
Occasionally, a message was appearing in VS saying:
"TF30063: You are not authorized to access xxxxdev.visualstudio.com."
Despite this, I had xxxxdev.visualstudio.com open in my browser and was able to work in it.
I tried many of the suggestions here. Then, chatting to a colleague, it turned out that my VS login had gone stale. In Visual Studio, I clicked on my icon in the top right and selected 'account settings'. There was an exclamation mark indicating that I had to refresh the login to my work account on visualstudio.com, using my xxxx email address.
Once I had done this the problem went away.
I hope this saves others the hassle I had this morning.

Nuget: Change package directory path and name [duplicate]

This question already has answers here:
Is it possible to change the location of packages for NuGet?
(19 answers)
Closed 5 years ago.
Is it possible to change the name and the path of the created nuget package directory?
My current folder structure is like this:
+src
++Project1
++Project2
++packages
I'd like to have a folder structure like this:
+Lib (packages)
+src
++Project1
++Project2
You can do this by creating a file called nuget.config and storing it in the same location as your solution file.
The config file should look like this:
<settings>
<repositoryPath>../libs/packages</repositoryPath>
</settings>
That's it.
Obviously you can put whatever path you want. Hope that helps.
I did it for VS 2010.
1) Get NuGet updated.
2) Add a nuget.config file to the solution folder like this:
<?xml version="1.0" encoding="utf-8"?>
<settings>
<repositoryPath>..\..\..\Dependencies\packages</repositoryPath>
</settings>
3) Create an empty "packages" folder at the location referred by the nuget.config file.
4) Go to Tools -> Library Package Manager-> Manage Nuget Packages for Solution
5) Type "asynctargetingpack" on the search box (or any other package you prefer)
6) Install the package
7) Go to the "packages" folder at the location referred by the nuget.config file and check the package files are there.
Starting with version 2.2 you can changed the NuGet.Config file.
NuGet Configuration File
Edit the value of the "repositorypath" key (and uncomment).
This limitation is on the VS plugin, using the command line you can choose the packages directory for the install/update commands, but then you can't go back to use the plugin UI.