Nuget update with PackageReference package management format - visual-studio

I'm using the PackageReference package management format available in VS2017 rather than packages.config.
The Nuget restore command works fine, however, the Nuget update seems to be searching from projects that have a packages.config even though I'm explicitly providing the .sln file
The command I'm using is
\NuGet\4.0.0\x64\nuget.exe update "Test.sln"
The output I get is
Scanning for projects...
MSBuild auto-detection: using msbuild version '15.3.409.57025' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin'.
Found 0 projects with a packages.config file.
Does anyone know if this should work or some other way of forcing my packages to update?

Does anyone know if this should work or some other way of forcing my packages to update?
At the moment, NuGet CLI does not support automatic package updates to the the new .NET Core .csproj format, you can refer to the below GitHub issue for detail:
https://github.com/NuGet/Home/issues/4358
If you want to force your packages to update, you can use the command line:
dotnet add package PackageName --version <version>
to update the package to the version that you specify. See the Github issue 4361 for detail.
Update to the Comment:
If you want to update to the latest version (without having to specify a specific version) of your nuget packages, you could use above command line without the option "--version":
dotnet add package PackageName
Besides, you can also use the command line update-package from the package manager console to update the package.

Related

NuGet reinstall updates the package to newer version

I have cleared all the local NuGet caches and Visual Studio 16.9 is unable to restore the packages.
It says:
All packages are already installed and there is nothing to restore.
When I build the project, I get many errors as the NuGet packages are not restored.
The project has to contain the exact same packages as they are in the PackageReferences inside the csproj file and I've read that using the Package Manager Console can help by executing the Update-Package -reinstall command.
The Install-Package command does not provide an option to force a reinstall, so use Update-Package -reinstall instead.
I tried it and now I have this in my log:
No package updates are available from the current package source for project....
Updates? I don't want to update anything...
Restored D:\Dev\MyProject01\MyProject01.csproj (in 29 ms).
Successfully uninstalled 'Acr.UserDialogs 7.1.0.475' from MyProject01
Successfully installed 'Acr.UserDialogs 7.1.0.481' to MyProject01
How is this a reinstall? Seems more like updating the package to a newer version...
How could I reinstall the same packages?
UPDATE:
When I use the Rebuild command in the Solution Explorer, this is what I see.
Actually, that right-click on the Solution-->Restore nuget packages is for the missing project packages folder with packages.config. And it cannot detect the global cache missing packets alone.
That is why Microsoft has enabled these two settings:
So you do not have to worry about restore step. Just click Rebuild button rather than Build button. The two restore settings are under Rebuild process.
The project has to contain the exact same packages as they are in the
PackageReferences inside the csproj file
update-package -reinstall command is for non-sdk style projects with packages.config nuget management format and always reinstall the same version. And I wonder if your solution has the non-sdk project that used packages.config and that project can use the command.
I have a non-sdk project with net framework 4.5.2. See:
But update-package -reinstall does not work on new-sdk projects.
ConsoleApp1 is a net core project.
So, I wonder if you use Update-Package under it. And that command is to update the old version to the new one which can works on both non-sdk projects and new-sdk projects.
Conclusion
When you face the problem next time, just click Rebuild button. It is more easier.
Try deleting your project's obj/ directory (this is where NuGet stores information about which packages it's decided to use), and then rerunning the restore.
If you have source dependencies (i.e. ProjectReference) you may need to delete their obj/ directories too

NuGet Command-line options for uninstalling/reinstalling packages for TFS/VS2013 and NuGet 3.4.3

I modified a solution and deleted one of the .csproj files and instead packaged the .dlls into a NuGet package. Then I added the package to the two other .csproj files that reference the .dlls. All was good - it builds locally, but I can't get it to build on the build server. When I look at the code gotten out of TFS for the build I see that the packages.config for both projects have the correct reference to the NuGet package, but when I open the solution in VS the references have little yellow exclamation marks next to them because they're broken references. The only way I can get it to build is to open the NuGet CMI and execute Uninstall-package package-name and the Install-Package package-name. Then the references are good. When I look in the packages.config of the main project it contains the correct reference to the Package. So I've given up on getting TFS to correctly grab the package, but since Install-Package and Uninstall-Package are CMI commands only I can't automate that (or can I)? Does anyone know if a way that I could automate that to happen after the source is pulled from TFS but before the build?
The yellow exclamation marks issue should be related to the reference path. When you download the source from TFS to another location, the system cannot find the references as the original reference path changed.
So, you need to reinstall the package, you can use the NuGet command line in the Package Manager Console:
Update-Package -reinstall
since Install-Package and Uninstall-Package are CMI commands only I can't automate that (or can I)? Does anyone know if a way that I could automate that to happen after the source is pulled from TFS but before the build?
The simple answer is you can not automate that. You can use the command Install-Package and Uninstall-Package to reinstall the packages to your project in the Package Manager Console, but it seems impossible to automate that. Please forgive me for the lengthy explanation below.
First, we need to know the different the operation Install packages between NuGet CLI and Package Manager, although NuGet CLI and Package Manager both support the operation Install packages.
The operation Install packages on NuGet CLI:
Obviously, NuGet will not reinstall the references when you using the Install-packages operation on the NuGet CLI, just download the package to the packages folder. See NuGet CLI referenceļ¼š
The install command does not modify a project file or packages.config;
in this way it's similar to restore in that it only adds packages to
disk but does not change a project's dependencies.
Conversely, operation Install packages on Package Manager:
Installs a package and its dependencies into a project.
If you want to automate that, you have to do this operations via NuGet CLI. Since Install package on NuGet CLI will not modify the reference of project, so we could not automate the operation install package to update the reference of the project.
Besides, we also do not recommend you automate that. Once you have automate that, NuGet execute the uninstall/install operations every time before you build the project. We only need to do an uninstall/install the operation after get the project from TFS. Even we do not need to do this operation if the references of the project are not broken after NuGet restore. So according to the reference of the project to determine whether or not to use an command:
Update-Package -reinstall
in the Package Manager Console should be the best choice.

Add Nuget reference to VS project package from command line

Is it possible to add a NuGet reference to an existing VS project (csproj or jsprox) using some command line tool?
I would need a functionality like package manager console offers using Install-Package command:
PM>Install-Package
This is not supported with NuGet.exe. With NuGet.exe you can download the NuGet packages based on what is in the packages.config file. You can also update NuGet packages and have their references updated in the project file by using NuGet.exe update. However you cannot use NuGet.exe to install the NuGet package so it adds the required references to the project file.
It is supported with Paket however if you use Paket then you would need to switch to using Paket for all NuGet packages since it has its own way of referencing the NuGet packages which does not include using the packages.config file. It also does not support PowerShell scripts.
I looked at installing NuGet Packages from the command line outside of Visual Studio using SharpDevelop and a set of PowerShell commands. This was a proof of concept but is not supported and requires most of SharpDevelop to be available.
If you are using .NET Core there is now a way to achieve this using the dotnet CLI.
dotnet add package EntityFramework
See Steve Smith's blog post for more information.

Does "enable nuget package restore" realy necessary for package recovery?

I can successfully restore package even if the option "enable nuget package restore" is not switched on in the project. Plus it generates additional files within my solution.
With the latest version of NuGet (2.7 or above) you do not need to use the Enable NuGet Package Restore menu option if you do not want to.
The latest version of NuGet, when installed in Visual Studio, will automatically restore any missing packages when your build your project. It does this without needing to add any new files to your solution and does not use MSBuild. It is now the recommended way of restoring NuGet packages.
You can also restore from the command line using NuGet with a command line similar to:
nuget restore YourSolution.sln

Reinstalling NuGet packages with NuGet installed as VS Extension

I would like to be able to install all of the NuGet packages in the packages.config, as per The NuGet docs. NuGet is installed as a VS Extension, and I can't seem to find nuget.exe. Is it possible to run:
nuget i packages.config -o Packages
Without maintaining a seperate copy of nuget.exe on a per project basis?
Reinstall all packages in all projects of the current solution:
Update-Package -Reinstall
You can find more information about reinstalling nuget packages here
Warning - using
Update-Package -Reinstall
or
Update-Package -Reinstall -IgnoreDependencies
may remove all of your packages and package.config files!
Always make sure that you have your backups performed first.
Scenario:
Solution with multiple projects
Each contains their own Nuget entries, some with the same packages (e.g., SharpRepository, Entity Framework)
Now copy folder without the packages folder for "distribution" somewhere else
Assume the packages folder wasn't included with the distribution
Now try the command Update-Package -Reinstall or if you have some alpha packages and/or are sure your dependencies are good, try Update-Package -Reinstall -IgnoreDependencies
Result:
Because the packages folder doesn't exist, the entries for your packages methodically go away, too. This can surprise some people - so be careful, is all I'm saying.
As an update to this post, NuGet 1.6 added support for the workflow to restore the packages at build time if missing.
Right-Click on the solution, click "Enable Package Restore Mode" to set it on.
More details at:
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
Found the solution on This blog entry. I needed to install NuGet.CommandLine, which makes nuget.exe. globally available in the VS command line. I can then set this up as a pre-build event to ensure that dependencies are downloaded.
A much easier option that you can keep enabled in Visual Studio during development to ease off your NuGet package installation related pain.
Keep both below mentioned options under NuGet Package Manager > General in checked state -
Allow NuGet to download missing packages
Automatically check for missing packages during build in Visual Studio
Have a look at the screenshot below:

Resources