How do you test a nuget package? - visual-studio

I am producing a nuget package (some c# dlls that are then built into a nuget package and then uploaded to our nuget server)
I would like to have a way to test the package before I bother uploading it to a server.
I have a test project that has the package installed
Is there a way (in command line etc) to uninstall the release package from the project, and install a candidate package from the local machine in its place? (to test that the new package build is actually good?).
It would be good to have a one click update and restore of this candidate package too, so if I do a code change in my code thats being packaged, I can just have a one-click "build -> package candidate package -> restore candidate package on the test project".
In short: I change code in my package. click a button, then just hit F5 in the test project and it runs with the latest code changes.

AFAK, to realize this, you have to modify the PackageVersion node to make such nuget package unique every time.
Assume that your lib project and main project are all new-sdk projects.
1) Add such node under the csproj of the class library project and make the PackageVersion unique. Also check this link:
<PropertyGroup>
<PackageVersion>1.0.0-re-$([System.DateTime]::Now.ToString('yyyyMMddHHmm'))</PackageVersion>
</PropertyGroup>
2) change your main project's csproj file to this:
<ItemGroup>
<PackageReference Include="Lib" Version="1.0.0-*" />
</ItemGroup>
It will use the latest version of the nuget under the nuget package source.
3) if you modify the lib project and then finish it, please right-click on the lib project Properties-->Pack to generate the new version.
4) then build the main project first, and then you can use the latest changed nuget package's code on the main project.
And if you want to restore to use the previous version of the nuget package, there is no way but you only have to install that version manually under Nuget Package Management.

Related

nuget.config ignores dependencies of dependencies (sub-dependencies)

I have a nuget.config for my project and have specified a repositoryPath. I also have specified the globalPackagesFolder.
<config>
<add key="globalPackagesFolder" value="c:\p" />
<add key="repositoryPath" value="c:\p" />
</config>
Now I want to install a third party package to my project. This package is located correctly at my specified path.
When I try to install another package, that has a dependency to System.Net.Http which has other sub-dependencies, nuget is not able to install this package, because the path is too long.
The direct dependecies are located in my specified repositoryPath from the nuget.config. The problem is, that sub-dependencies are not located at this path. A file search in this repositories resulted in no matches.
The only reason I specified a repositoryPath in nuget.config is that I wanted to move the sub-dependencies to another folder, because otherwise the path is too long and nuget can't install the package. This is not a problem for my co-workers.
How can I change this behavior?
If this is not possible, is there another solution to my problem?
A co-worker sugested to shorten my Windows login, but this is not a solution. I also can't shorten our Teamcity user login name.
The package I try to install is developed by my company. So if I have to change settings in the package, this is not a problem.
I tried with Visual Studio 2017 Pro and Enterprise with Nuget 4.6.0 and Visual Studio 2019 RC with Nuget 5.0.0.
Target Framework is .NET Core 2.1
When I add the package System.Net.Http to my project manualy and install the package, there is no problem, but i would rather don't do this, unless there is no other solution.
Edit:
To clarify the situation: If I install Package A that has a dependency to package B and B has a dependency to Package C, Package A is in my specified Path, but B and C are not. I don't even know, where Package B and C are located.
nuget.config ignores dependencies of dependencies (sub-dependencies)
This is the correct behavior for .NET Core project.
In dotnet core, or projects that use PackageReference, reference to only the immediate dependencies are listed and only download the immediate dependencies to the package cache.
That is the reason why you install Package A that has a dependency to package B and B has a dependency to Package C, Package A is show up in your project file like:
<PackageReference Include="PackageA" Version="1.0.0" />
and cache in your specified Path, but B and C are not.
This is a one of the biggest advantages of PackageReference where we don't clutter the project file with gazillion dependencies which the consumer probably doesn't care about. Install your nuget package to a project, navigate to the project_root/obj/project.assets.json and open this json, you'd see your package listed along with its dependencies. If you see your intended dependencies here, it validates that package is authored correctly. But NuGet will not restore the dependencies to the packages cache unless you install these dependencies directly.
Hope this helps.

Nuget package did not install on target machine

I've created a windows service that uses Microsoft Visual Studio Installer Projects to install the service. The service was running fine until it needed the references from the Nuget package Microsoft.SqlServer.SqlManagementObjects
How do I add this package to the setup project so the references are available or can I simply install the nuget package on the target machine?
How do I add this package to the setup project so the references are available or can I simply install the nuget package on the target machine?
Since nuget does not support the setup project, so we could not add that nuget package to the setup project directly.
To resolve this issue, the most direct method is manually add the .dll files in nuget package to the setup project (Right click the setup project->Add->Assemble...->Browse).
If manually adding the .dll file is not what you want, you can add a blank library project to the setup solution, then add the that nuget package to the library project.
Add the Project Output of the library project to the setup project (Add->Project Output...->Primary output):
In this case, all the .dll files in the nuget package are available for your setup project.
Hope this helps.

Is NuGet automatic package restore in Visual Studio 2015 a fairy tale/mythical beast?

I have been trying to figure out why automatic package restore in Visual Studio 2015 doesn't work.
As I understand it there is nothing to do but check a couple of settings. When you build, it looks for missing packages and downloads them automatically.
I have a solution that has 15 individual projects. The majority of them will not compile because of "missing packages".
I do not have any of the legacy NuGet (.nuget folder etc.) in any of these projects and I have the latest and greatest version of NuGet.
Visual Studio simply will not download the missing files. I deleted the solution package folder and it does re-create and download all of the packages when I build, but each individual project still shows missing references.
If I go to the package manager console and issue a
Update-Package -reinstall
then the packages download and everything works. I'm just wonder why it doesn't do this automatically.
It's supposed to right?
NuGet Restore only restores files in the packages directory, but does not restore files inside your project or modify your project.
For example, if there has a package will add some reference dlls or add some content files into your project. After deleting these reference dlls and content files from your project, it will not be added when restoring this package. This will cause the your project could not find the missing pacakges when compile.
So we need use "Update-Package -reinstall" command to force reinstall the package references and content files into project.
Update the example for sharing projects in a team:
Following is my solution structure, the CommDLL project installed some NuGet packages and entire solution is managed by a source control.
I download this solution on another machine from source control use another user account and install another NuGet packages into the CommDLL project. Then use some content from the new installed package and build the project successful. Please make sure the package dlls has been added into your project and it has been set the correct HintPath in .csproj file.
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
Next, I check in this modified project into the source control. Please make sure you the .csproj file and packages.config file are checked in.
Now I get the latest version on another machine to get the modified content. After check out the latest version from source control, the package references are shown with a warning because the project is not compiled, which means the packages are not restored. Please rebuild this project, it will restore the packages for your project (make sure your Visual Studio 2015 has enable "Allow NuGet to download missing packages" and "Automatically check for missing packages during build in Visual Studio" before rebuild this project).

Nuget Packager task - dependency is not added

I'm using Visual Studio Team Services to automate the creation of a nuget package. This package has a dependency on another nuget package (Newtonsoft.Json v8.0.3). I have configured a Nuget Packager task as part of the build:
But the generated package doesn't contain the reference to the nuget package:
Running the following script on my local machine to generate the package:
nuget pack myproject.csproj -Symbols -IncludeReferencedProjects -Properties Configuration=Release -OutputDirectory bin/Release
I can see that the dependency is added to the generated package:
What am I missing here? Why the dependencies are not being added to to package that is being generated on the build server?
"IncludeReferencedProjects" is used to add the referenced project to the nuget package, not the referenced nuget packages.
Include referenced projects either as dependencies or as part of the
package. If a referenced project has a corresponding nuspec file that
has the same name as the project, then that referenced project is
added as a dependency. Otherwise, the referenced project is added as
part of the package. Learn more at NuGet.org.
When you create the nuget package for the project which referenced to another nuget package. The nuget package dependency will be added automatically if the nupkg file exists in the packages folder. For your issue, you can add a "NuGet Installer" task in your build definition to restore the nuget package files for your solution. And then when you create the nuget package for your project, the referenced nuget package will be added as dependency.
I have found a solution for my problem, even though I don't understand why the package was not being created properly.
So basically I have 2 builds:
a "generic" build that will compile the entire solution and run unit tests, etc (automatic, triggered on commit)
a build to generate the nuget package (manual)
This problem is on the 2nd build. I was trying to understand what were the differences between creating the package manually on my local machine and creating the package on the build server and I realized that on the build server I was not getting the source code for the solution, only for the project folder and the .nuget folder. Given that I have more than 40 projects in the solution it makes no sense to get the whole source code for the 2nd build, right? But for some reason this is not enough to generate the nuget package properly.
So, the solution to my problem was to get the source code for the following files/folders:
Project folder
.nuget folder (needed when restoring/installing the missing packages)
Solution file (mysolution.sln)

Nuget restore doesn't add reference to project

Nuget keeps on not doing what I expect:
Say I got a bunch of references in a packages.config and add that to a newly created project. Then nuget asks me to restore the packages, on answering yes, it only downloads the packages to the packages folder but forgets to add a reference the the project.
How to automatically also add a reference to my project once restored?
Lasse is correct about the NuGet restore. NuGet restore will only download the NuGet packages and extract them to the packages directory.
However with just the package information in the packages.config file there is a way to have NuGet add the references to the project.
First restore the NuGet packages.
Next open the NuGet Package Manager Console and reinstall the packages using:
Update-Package -reinstall
The above -reinstall command will uninstall and install the packages again and fix any missing references.
There are some limitations with the above command which are documented on the NuGet site.
Restoring packages will only download the packages and place them into the right folders on disk.
You get the references when you originally add the nuget packages to the project the correct way.
You should not add nuget packages to the projects by modifying the package configuration file, you should add them using the "Add Nuget reference" menu item on the References folder of the project.

Resources