Install nuget package with a VSPackage - c#

I'm trying to install a nuget package into a project that I am generating with a VSPackage. So far, I am able to create a solution from a project template:
Solution4 soln = (Solution4)ApplicationObject.Solution;
string prjPath = "C:\\MyProject";
string templatePath = soln.GetProjectTemplate(#"SomeProject\MyTemplate.vstemplate", "CSharp");
soln.AddFromTemplate(templatePath, prjPath, "New CSharp Project", false);
But now I need to be able to install a nuget package into that project as well. The package is located online, for example, https://somewhere.mydomain.com/nuget/feed , and has the ID PackageX.

You'll need to perform the same steps that "NuGet Package Restore" does, adding NuGet.exe, NuGet.config and NuGet.targets correctly to the solution. To see how this changes things, diff two empty solutions where one restores NuGet packages and the other doesn't.
To add your custom feed, you can add
<packageSources>
<add key="local" value="https://somwhere.mydomain.com/nuget/feed"/>
</packageSources>
to NuGet.config under the <configuration> node.
Finally, add the package name and version to packages.config in the vcproj directory and you should be all set!

Related

How to add public packages from github to my .Net solution

I am trying to add packages from https://github.com/orgs/DKE-Data/packages to my asp.net project. Here are the things I have tried
Download the package.nupkg file and add the location to the package manager source but it does not allow me to install the package.
Add the PackageReference to .csproj file and did a restore - did not work
As these packages are publicly available isn't there a straight forward approach to add them to my packages?
Appreciate any help here!
In order to use this feed you need to configure it. I suggest creating a nuget.config file next your solution file (.sln) with the following contents:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="github-DKE-Data" value="https://nuget.pkg.github.com/DKE-Data/index.json" />
</packageSources>
</configuration>
Then (maybe after re-opening the solution in visual studio or other IDEs) you should be able to add the pacakge via the NuGet UI or directly in the csproj file.
For more information on how to use GitHub packages for NuGet see Configuring dotnet CLI for use with GitHub Packages as you may need to set up authentication as well.

Why is NuGet looking at global cache but hintpath points to ..\packages

My project's .csproj file has hintpaths for packages set to ..\packages\etc...
NuGet says all packages are available as I have the packages in my global cache under C:\Users\\AppData\NuGet Packages\packages
Each package in the references folder has a yellow warning sign as there is no ..\packages folder.
I can move the packages from the global cache and manually create the ..\packages folder or point the hintpaths to the global cache folder to get the project running, but then my builds fail in CI/CD.
I am using Visual Studio 2017 15.5.5, NuGet Package Manager 4.5.0
The error message:
This project references NuGet package(s) that are missing on this
computer. Use NuGet Package Restore to download them. For more
information, see http://go.microsoft.com/fwlink/?LinkID=322105. The
missing file is
..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props.
Why isn't NuGet trying to create the ..\packages folder?
In my NuGet.config there was a setting:
<add key="repositoryPath" value="..\..\NuGet Packages\packages" />
No idea why that was there. Once I removed this everything went back to normal creating a solution level packages folder

What is the best way to restore nuget packages?

I have a solution that uses custom nuget package sources. For now I specify them in Nuget.config file that is located near my solution file(so that it is checked out from source control):
|- MySoulution.sln
|- MyProjFolder
|- .nuget
|- Nuget.exe
|- Nuget.config
|- Nuget.targets
This works well when building solution from VisualStudio. It manages to read this nuget.config file and successfully restore all packages.
Now I'm configuring my solution to be built from TeamCity. So I've added project configuration and a build step to build it. But TeamCity doesn't restore nuget packages by default. So I've added a separate Nuget installer build step that runs nuget(of specific version) restore for my solution. But the problem is that it doesn't seem to see my custom nuget package sources from Nuget.config file in .nuget folder next to solution file.
I see two possible ways to overcome this:
Configure my custom package sources inside Nuget installer build step.
Configure my custom package sources in Nuget.config in AppData folder on build machine.
I don't like neither of this approaches because they don't provide me single poing of configuration for building both from TeamCity and VisualStudio.
To sum up, the question is: how do I configure my custom package sources so that they would be visible both from TeamCity and VisualStudio without requiring me to configure them several times in different places?
how do I configure my custom package sources so that they would be visible both from TeamCity and VisualStudio without requiring me to configure them several times in different places?
As you know, if you do not want to configure custom nuget sources several times in different places, you can set the custom nuget sources in the NuGet.config and add it to source control. So the key to your problem is why NuGet doesn't respect the your custom nuget package sources from Nuget.config file in .nuget folder next to solution file.
Just as my comment, if you're using NuGet 2.7 or later and have a solution that is still configured for MSBuild-integrated restore, you may have an older version of nuget.exe in the solution's .nuget folder. This will cause builds to fail with an error stating that you have not given consent to restore packages.
To avoid this issue, it's recommended to migrate any project using MSBuild-integrated restore to use the automatic restore capabilities of NuGet 2.7 and above, you can follow the process as below:
Close Visual Studio to avoid file potential file locks and conflicts.
If using TFS:
Remove nuget.exe and NuGet.targets from the solution's .nuget folder and remove those files from the solution workspace.
Retain Nuget.Config with the disableSourceControlIntegration setting as explained in Omitting packages with Team Foundation Version Control.
If not using TFS:
Remove the .nuget folder from the solution and the solution workspace.
Edit each project file in the solution, remove the element, and remove any references to the NuGet.targets file. Those settings generally appear as follows:
After that put NuGet.config next to the solution file with custom NuGet source:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<packageSources>
<add key="CustomSource" value="http://CustomSource/nuget" />
</packageSources>
<packageRestore>
<add key="enabled" value="True" />
</packageRestore>
</configuration>
Now, in the NuGet Installer step, there is now a "Package Sources" field that you can fill in to have team city use a custom feed:
You can refer to this document NuGet Package Restore with TeamCity for more detail.
Besides, we can also specify custom feed in the NuGet.targets file in the .nuget folder(I did not verify it yet):
<ItemGroup Condition=" '$(PackageSources)' == '' ">
<PackageSource Include="https://nuget.org/api/v2/" />
<PackageSource Include="\\MyShare" />
<PackageSource Include="http://MyServer/" />
</ItemGroup>

nuget and vsix repo

I have exported a template project using the export tool in VS2015. I have created a nuget package which this project rely on.
In the vstemplate file I have added:
<WizardExtension>
<Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
<FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
</WizardExtension>
<WizardData>
<packages repository="extension" repositoryId="ViSoftVSIXPackage.Visoft.87c53ff4-9d1f-xxxxxxxxxxxxxx">
<package id="ViSoftCore" version="1.0.0" />
</packages>
</WizardData>
The repositoryId is the Id of the VSIX project.
In the VSIX project I have added the assets:
<Assets>
<Asset Type="ViSoftCore.1.0.0.nupkg" d:Source="File" Path="Packages\ViSoftCore.1.0.0.nupkg" d:VsixSubPath="Packages" />
<Asset Type="Microsoft.VisualStudio.ProjectTemplate" d:Source="File" Path="ProjectTemplates" d:TargetPath="ProjectTemplates\HMIViSoft.zip" />
</Assets>
Now, when i install the vsix, create a new project I get the following:
NuGet Package restore failed for project HMI.ViSoft4: Unable to find version '1.0.0' of package 'ViSoftCore.
When I go to Nuget Package Manager it says the package is installed but not able to find source.
I have tried this in many ways and I can't get this to work automaticly. If i manually create a package source in nuget package manager to c:\packagelocation everything is all good.
What am i missing here? Is this not supported? Do I write code in the vsix to create a package source in the nuget package manager?
I faced the same issue - Step 11 highlighted in this answer helped fix the problem. Basically this step ensures that a copy of nupkg file is created under <template install path>/Packages/

Adding NuGet package present on hard drive (Not hosted anywhere) to a project

MY Task is to generate a NuGet package of one project containing static content and binaries. Then means by which this NuGet package can be consumed in another Project/ Solution.
I have completed the first half of the task where i was able to generate a NuGet package out of a project, but not sure how to consume this inside another project/ solution. I see only option of adding already published Projects from NuGet repository but not anything which is out there on file system/ hard drive.
Any help is appreciated.
Thanks in advance.
You need to add the directory which contains the package as a NuGet Package Source. In Visual Studio do the following
Tools -> Options
Package Manager -> Package Sources
Add the file system location and hit "Update"
After this the local package should appear in the Package Library Manager
If you are looking to change the NuGet.exe command line then create a Nuget.exe.config file and add the following
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="LocalName" value="path/to/your/package" />
</packageSources>
</configuration>

Categories

Resources