visual studio code and private nuget - nuget

I have a library that comes from a private nuget feed. I have the url and credentials for that but dont know how to connect to the feed properly with visual studio code. I am using dotnetcore framework.
I created a Nuget.Config file in the root of my console application with the feed url and username and password but this didn't seem to pick up the packages from that feed when imputting them in the project.json. Even doing a restore would produce errors.
Does anyone have an example of how they would set up a project to do this? I know it is not normal to have the Nuget.Config file in the project but this is a test project so would not live there once the project got past proof of concept.
My nuget.config looked like this
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="CustomRepo" value="https://nuget.feed/nuget/" />
</packageSources>
<!-- Used to store credentials -->
<packageSourceCredentials>
<CustomRepo>
<add key="Username" value="something" />
<add key="ClearTextPassword" value="thepassword" />
</CustomRepo>
</packageSourceCredentials>
<!-- Used to disable package sources -->
<disabledPackageSources />
</configuration>

Apoligies this is now resolved and working with the nuget.config file in the root folder. I dont know what I did. Only thing I did was re-type the whole xml but dont know what what would have done. Anyway it is working which is great.

Related

How do I include NuGet packages in my solution for .Net Core projects?

With classic .Net projects, if I added a reference to a NuGet package, it would get downloaded to a packages folder and I could check that into source control along with the rest of my code. This allowed any developer to download the code, along with the NuGet packages, without having to set up a package source to separately download the packages. This is not how .Net Core projects work. There does not seem to be a packages folder for the solution, and it is up to each developer to set up the custom package source and download the packages when they get the code. Is there a way to configure the .Net Core project to do like the classic .Net projects did and manage a packages folder?
A lot of NuGet behaviour can be controlled via NuGet.Config files (See this reference for more details)
If you place a NuGet.Config file next to the solution with the following content, you can override the location that the packages will be restored into:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\packages" />
</config>
</configuration>
If the problem is that you'd need to set up additional sources in VS on every machine, you can also add those sources via a NuGet.Config in your repository so VS will pick up the feeds to use when opening a solution:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="CompanyFeed" value="https://my.company.com/private/nuget" />
</packageSources>
</configuration>
If you have no feed to host packages and need to include packages with the solution, you can use a directory containing .nupkg files as well in NuGet.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value=".\NuGetPackages" />
</packageSources>
</configuration>

Add custom package source to Visual Studio Code

Does anybody know how to add custom package source to Visual Studio Code?
E.g. I'd like to add https://www.myget.org/F/aspnet-contrib/api/v3/index.json as a package source and drive these packages through project.json.
To add to the answer, adding a nuget.config in the project solves it for the project. Adding to the root is ok. The config could look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="MyGet" value="https://www.myget.org/F/aspnet-contrib/api/v3/index.json" />
</packageSources>
</configuration>
Another option that worked for me and was actually needed as part of my CI/CD pipeline is to use dotnet nuget add source <repo-url> --name <repo-name>
Simply call that before you call dotnet restore or dotnet build
Reference:
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-add-source
Note - if your custom package source is password protected, then the credentials should also be part of the config file.
<packageSourceCredentials>
<MyGet> <!--package src name-->
<add key="Username" value="something" />
<add key="ClearTextPassword" value="thepassword" />
</MyGet>
</packageSourceCredentials>
And for anyone wondering why the credentials have to be in clear text (which defeats the whole purpose of having credentials in the first place, since this config file will also go into source control).
This is an open issue in dotnet / nuget cli. Ref github issue # 5909 & 1851
You can add a NuGet.config file and specify the package source in there. Some reference docs: https://learn.microsoft.com/en-us/nuget/schema/nuget-config-file

Can I get the location of the packages folder for the current solution when applying an xdt?

I want to install a design time plugin for a visual studio extension (ie runs when using visual studio, and not when the application is running). This requires that I modify the app.config with an entry that points at the dll containing the extension code. The plugin is installed via nuget and if I add this dll to a folder under the project and use a fixed path in the app.config.install.xdt then everything work ok. However what I want is for the xdt to insert a value which points to the dll in the packages folder, where it gets installed via nuget. My problem is that the relative path to the nuget folder is not fixed for the projects. Each project may be in a different folder (several folders deep inside the solution folder) and not just in a direct child of the solution folder, so I want to be able to use some variable in my xdt.
Currently I have this in my xdt:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<specFlow>
<plugins xdt:Transform="InsertIfMissing">
<add name="NCrunch.Generator" path=".\SpecflowPlugins\" type="Generator" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
</plugins>
</specFlow>
</configuration>
but I want something like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<specFlow>
<plugins xdt:Transform="InsertIfMissing">
<add name="NCrunch.Generator" path="$(SolutionDir)\packages\Specflow.Ncrunch.1.0.0\lib\net" type="Generator" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
</plugins>
</specFlow>
</configuration>
is this possible in xdt? if not is there another option for getting this to munge the app.config correctly when my nuget package installs?
The XDT syntax doesn't allow replacement tokens such as the one you put in your example.
One option I can think of would be to modify the app.config.install.xdt file in your init.ps1 PowerShell script, replacing the tokens for the actual values before the XDT transform is applied by NuGet.
For more information regarding init.ps1 see: Creating and Publishing a Package

Setting an environment variable in a NuGet.Config file

I have a situation where I want to set the repositoryPath variable of the NuGet.config file to a location relative to the current user's machine. The goal is to have a location where all of the NuGet packages get placed, so that:
They are not located in the solution folder for each of our team projects (ie. /Solution/packages/*)
They are shared across many projects, so that only one copy of that package needs to be installed on the machine.
Ideally, I would like to use a path with an environment variable, such as %APPDATA%, however the NuGet Package Manager doesn't work with this.
My config file looks something like this:
<config>
<add key="repositoryPath" value="C:\External\NuGetPackages" />
<add key="DefaultPushSource" value="\\SourceCode\NuGetPackages\" />
</config>
Where ideally I would like the repositoryPath to work like this:
<add key="repositoryPath" value="%APPDATA%\External\NuGetPackages" />
Things I have tried:
$(APPDATA)
$APPDATA
$Env:APPDATA
The first two of these just result in directories with those names being placed in the same location as the NuGet.config file.
This has been added as a feature in NuGet v3.4+
Variables can now be added in Windows using the standard syntax, eg:
<configuration>
<config>
<add key="repositoryPath" value="%HOME%\NuGetRepository" />
</config>
</configuration>
See https://docs.nuget.org/consume/nuget-config-file#environment-variables-in-configuration

Nuget package restore not respecting my repository path configuration?

I have the following setup in my solution's .nuget/Nuget.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<config>
<add key="repositoryPath" value="C:\Xos\packages\" />
</config>
</configuration>
The point of this is to try and make Nuget package restore manageable, and share packages across our many solutions. I then went into my current solution's packages directory and deleted all the packages (so they would instead restore them to C:\Xos\packages). I then closed VS 2012 and reloaded the solution and rebuilt.
However, all my packages are being restored in my solution's packages directory, NOT the directory specified in repositoryPath configuration.
What am I doing wrong?
I had the same issue with VS2015 RC. One of two things suddenly made it work:
(1) I checked-out the project file from source control, and completely closed Visual Studio (not just the solution).
(2) I edited the csproj file and noticed that the entry for packages.config had a <SubType>Designer</SubType> inner tag, which I removed.
I suspect it was the close-down of Visual Studio...
This appears to be a bug and is fixed in 2.3 (which doesn't appear to be released yet).
Until then I modified my Nuget.Targets as described in that bug and using that for now.