Local Nuget Mirror - c#

For a current project, we had to resort to C# for parts of the programming. After the leftpad-occurences in npm, I'd like to create a mirrored package repository for securing builds.
Since managing dependencies by hand is a hassle, is there an automated way to mirror a NuGet-Package including all dependencies on my local / shared drive?

You can setup a private nuget feed and host the packages there.
For example in azure devops we have Azure artifacts:
https://learn.microsoft.com/en-us/azure/devops/artifacts/start-using-azure-artifacts?view=azure-devops#get-started-with-azure-artifacts
In that case the nuget feed is a private feed and you can configure it to access the public nuget feed. any consumed packages will then also be kept on the private azure artifacts feed, meaning you always have the original package.
The nuget.org upstream source allows you to merge the contents of nuget.org into your feed such that the NuGet client can install packages from both locations without making multiple search queries. Enabling upstream sources also automatically enables saving of packages you use from the upstream source.
https://learn.microsoft.com/en-us/azure/devops/artifacts/nuget/upstream-sources?view=azure-devops
Also see:
https://learn.microsoft.com/en-us/azure/devops/artifacts/concepts/upstream-sources?view=azure-devops
I've also seen people using MyGet:
https://www.myget.org/
And another option is to just use a local folder as a nuget source.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- NuGet packages are now stored globally, see: https://stackoverflow.com/a/35809007/4122889 -->
<!-- see: https://stackoverflow.com/a/53451805/4122889 -->
<config>
<add key="globalPackagesFolder" value=".\Packages" />
<add key="repositoryPath" value=".\Packages" />
</config>
<!-- Setup for local packages, not in a nuget source -->
<packageSources>
<add key="My_Azure_Feed" value="https://pkgs.dev.azure.com/org/proj/_packaging/My_Azure_Feed/nuget/v3/index.json" />
<!--<add key="LocalPackages" value="./LocalPackages" />-->
</packageSources>
<activePackageSource>
<!-- this tells that all of them are active -->
<!--<add key="All" value="(Aggregate source)" />-->
</activePackageSource>
</configuration>

Related

Google Cloud build and private Nuget repository

I have a private NuGet repository created with BaGet.
My software is hosted in Google Cloud where there is also the Git repository with my code.
I have some Cloud build triggers that run the build in the cloud and automatically deploy the applications.
I have created some NuGet package with my projects and now I need to change the reference from code (project reference) to NuGet package reference in my private repository.
Locally all works, but now I need to do the same in my Google Cloud build trigger and I don't know where I can specify the reference to my private repository.
My build script is a yaml file but inside I don't have a reference to the standard NuGet repository and I don't know how I can specify an additional NuGet package repository for some libraries.
NuGet reads configuration from nuget.config files. In fact, it reads from multiple config files. So you can commit a nuget.config to your source code repository, and therefore anyone else who clones your repo (even a CI agent) doesn't need to be configured, it "Just Works".
The part of the config that's relevant to your question is the <packageSources> section, so a minimal config with only package sources defined is:
<configuration>
<packageSources>
<!-- make sure other nuget.config files don't add unexpected sources -->
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="private" value="https://my-server/index.json" />
</packageSources>
</configuration>

NuGet package is not being installed when reading from TFS

We are using Visual Studio 2019 and we have a solution that has a references to some NuGet packages either from nuget.org or from our private server we are managing the solution with packages.config management, the problem is there is one ASP.NET project in that solution that does not install any of the required packages(in our case it misses the NewtonSoft.json), and installs all the other ones correctly, even though we have packages restore enabled, and there are other ASP.Net projects in the solution that install the Newtonsoft package correctly, but each time you read the last version of the solution from zero (in the case you delete the local solution and re-read it) from the TFS, you need to install the Newtonsoft.json manually for that project specifically.
PS: it's a problem that can be solved in 5 min every time but I know something is wrong and I have been trying to understand what is it, I don't want to solve it localy, but I need a solution the resolve it on the TFS level, so please help me if you can or if you know any other questions here that can help, please guide me through (if I didn't see it already).
My NuGet.Config document:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
<add key="MxNuget" value="our server path" />
</packageSources>
<activePackageSource>
<add key="All"
value="(Aggregate source)" />
</activePackageSource>
<packageRestore>
<add key="enabled" value="true" />
<add key="automatic" value="true" />
</packageRestore>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
the error we are getting is the following:
The name 'Newtonsoft' does not exist in the current context.
The problem I was having was because the TFS was not restoring all the packages needed because when he sees the Folder 'Packages' in the solution he just check if the package is inside, and when he is done, the version that he found and the one on the local machine may be different and that was causing the problem, so I wanted to delete my packages folder on the TFS but I couldn't find it. For that I installed a package in my solution which adds .tfignore to the .nuget folder in the solution directory, and that last file tells the TFS to ignore the packages file on the server and restor them always.
Questions that were useful:
1 - Tfs Can't restore Nuget packages.
2- NuGet Packages are missing
3- External: Missing Nuget Packages on TFS Build Server
I resolved the problem by following the steps:
installed the package DisableSourceControlIntegration to the project that was missing the Newtonsoft.json NuGet.
Unified all the versions of NewtonSoft.json on the projects to which my ASP.Net project has a reference. (I think that was the main problem because I had them unified before but I updated the whole group with their config files).
In the visual studio, open Tools --> Nuget package manager --> package manager setting and make sure that the Packages.config is selected as the format of managing the NuGet packages.
deleted all the packages cash.
Re-compiled the solution.
Pushed to the TFS.

How can I configure a local feed for nuget packages in VSCode?

I'm using VSCode with the official C# extension to develop a .NET Core console app on Ubuntu. I'd like to use a private nuget package in this console app. Usually one would add a local feed for nuget packages like described here with nuget init c:\packages \\myserver\packages. I've tried to find some settings in the C# extension which would allow me to configure a local feed for nuget packages but did not find any. Is it possible to do the same with the VSCode + C# extension builtin support?
Alternatively, you can create a file named "NuGet.Config" in the directory where the solution is located and do its contents as follows;
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="KetumMyGet" value="https://www.myget.org/F/ketum/api/v3/index.json" />
<add key="KetumMyBaget" value="http://localhost:5000/v3/index.json" />
<add key="TestSource" value="c:\packages" />
</packageSources>
</configuration>
See more here

VSTS unable to load the service index for source 401

I'm aware of the other posts about the same signature. I still can't resolve my issue after going thru them.
My team uses VSTS's build definition for continuous integration.
This build definition works fine until the lastes pull request.
I'm running into the error msg below during the Nuget Restore
2018-06-20T00:37:27.6438127Z System.AggregateException: One or more errors occurred. ---> NuGet.Protocol.Core.Types.FatalProtocolException: Unable to load the service index for source https://microsoft.pkgs.visualstudio.com/_packaging/CBT/nuget/v3/index.json. ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
I do have https://microsoft.pkgs.visualstudio.com/_packaging/CBT/nuget/v3/index.json in the nuget.config, and there is nothing changed in the nuget.config in the failing PR
I can nuget restore and build the entire solution successfully on my local machine using VS2017. The only related change in the PR is that instead of using package.config, it uses packagereference to get the nuget package. I tried to move back to using package.config, the build would still fail with the same error msg.
Thanks in advance.
You can update the VSTS feed with credentail (PAT or alternate credential) in the specified nuget.config file.
Such as:
nuget sources update -Name "vstsfeed" -Source https://microsoft.pkgs.visualstudio.com/_packaging/CBT/nuget/v3/index.json -Username "Alternate username" -Password "alternate password" -configfile /path/to/nuget.config
Then you can commit the changes for the nuget.config file and push to VSTS repo. And build again to check if it works.
You can use a command as shown in the accepted answer - also, you can add the feed in a nuget.config file placed in the root of your repo
Notice, this shows how to add credentials for a custom feed with spaces in the feed name: My Nuget Artifacts
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageRestore>
<!-- Allow NuGet to download missing packages -->
<add key="enabled" value="True" />
<!-- Automatically check for missing packages during build in Visual Studio -->
<add key="automatic" value="True" />
</packageRestore>
<packageSources>
<add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
<add key="My Nuget Artifacts" value="https://pkgs.dev.azure.com/ConsotoOrg/_packaging/Consoto/nuget/v3/index.json" />
</packageSources>
<packageSourceCredentials>
<My_x0020_NuGet_x0020_Artifacts>
<add key="Username" value="justme" />
<add key="ClearTextPassword" value="xyzyoqyvslyfs1t1khru6wd33gebujhpr9moocbujfhv8ukxtxyz" />
</My_x0020_NuGet_x0020_Artifacts>
</packageSourceCredentials>
<activePackageSource>
<add key="All" value="(Aggregate source)" />
</activePackageSource>
<!--
Used to specify trusted signers to allow during signature verification.
See: nuget.exe help trusted-signers
-->
<trustedSigners>
<author name="microsoft">
<certificate fingerprint="3F9001EA83C560D712C24CF213C3D312CB3BFF51EE89435D3430BD06B5D0EECE" hashAlgorithm="SHA256" allowUntrustedRoot="false" />
</author>
<repository name="nuget.org" serviceIndex="https://api.nuget.org/v3/index.json">
<certificate fingerprint="0E5F38F57DC1BCC806D8494F4F90FBCEDD988B46760709CBEEC6F4219AA6157D" hashAlgorithm="SHA256" allowUntrustedRoot="false" />
<owners>microsoft;aspnet;nuget</owners>
</repository>
</trustedSigners>
</configuration>
FWIW - In recent days, I've found I can clear up some NuGet restore errors by removing the trustedSigners section from the config
This is not what the problem was with the OP but I'm adding a note here since I found this question while searching for the same error message.
In our case we were building a .net 5 app for in Azure DevOps. We had to upgrade to a newer version of NuGet using the NuGet tool installer. Then the restore worked just fine using the .Net Core task with the restore command.

How can I restore NuGet packages from a custom package source when using LINQPAD's lprun.exe?

We use the lprun feature of LINQPAD to run our build scripts; However as we are in a corporate environment we block access to the NuGet.org package source:
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
And instead use a corporate proxy e.g. a custom package source:
<add key="MyProxy" value="https://proxy.server/custom-nuget-repo" />
When developing locally these sources are specified in:
C:\Users\Me\AppData\Roaming\Nuget.config
However LINQPAD is ignoring this file, how can I provide custom package sources when using the lprun to restore NuGet packages referenced?
From newer LinqPad versions this is built in to the LinqPad NuGet manager. Go to QueryProperties (F4) -> 'Add Nuget' -> 'Settings' and new package sources can be added just as easily as in VS.
Adding a new answer here since Google led me to this question first when trying to find the answer.
lprun and LINQPad both use the file %AppData%\LINQPad\NuGetSources.xml for NuGet Package sources, so this is the file you need to edit.
In case this file doesn't exist, here is an example:
<?xml version="1.0" encoding="utf-8"?>
<NuGetSources>
<Source Name="(default)" />
<Source Name="My Packages" URI="https://www.myget.org/F/your-username-here/" UserName="Example" Password="base64encodedstring" Enabled="false" />
</NuGetSources>

Categories