NuGet package is not being installed when reading from TFS - c#

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.

Related

Change nuget packages folder and use another NuGet.Config in Visual Studio

I want to change nuget package folder but it doesn't work.
I tried very much tricks but not work.
I restart many times VS but not work.
My config:
Visual Studio Community 2019
Windows 10 1909 x64
I have a VS solution folder
MyProjectSln\
HelloWorld\
bin
nuget_packages
NuGet.Config
I want VS studio nuget package manager console use the "NuGet.Config" file and put all packages downloaded in "nuget_packages"
So nuget packages must be in D:\MyProjetSln\HelloWorld\nuget_packages.
Content example of D:\MyProjetSln\HelloWorld\NuGet.Config :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\nuget_packages" />
<!-- I have also tried with repositoryPath but not work -->
</config>
</configuration>
The xml syntax is correct in my case (in my file).
Example, when i execute the command "install-package NUnit" from Package Manager Console, it put downloaded packages in D:\MyProjectSln\packages\ and I don't want that.
Thank you for helping !
Just as this document said, the new nuget.config file must be under the same level directory of the project folder rather than put the file inside the project folder.
In other words, it must be located in at least the solution directory or a higher-level directory.
Note: if you use this way, it will act on all the projects in the same level directory and all the projects in the sub directory.
So you should put the new nuget.config file on the D:\MyProjetSln.
Then modify its content as:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<config>
<add key="repositoryPath" value="HelloWorld\nuget_packages" />
</config>
</configuration>
Then, close VS and then reopen your project to enable that function.
In my side,
At the time of writing this message, what I want is not possible at the moment.
Maybe it will be a feature later.

Azure Devops build not restoring automapper nuget

I am trying to build a .Net project but it doesn't find Automapper v3.3.1.
I have the following build steps:
Use NuGet 4.3.0
NuGet restore
Build solution ...
When running step 3 it gives the error:
Error CS0246: The type or namespace name 'AutoMapper' could not be
found (are you missing a using directive or an assembly reference?)
In the Nuget Restore step I have the
Path to solution, packages.config, or project.json set to the .sln file.
Under Feeds and authentication I checked the option Feeds and in my NuGet.config I left path to NuGet.config empty since I have a global Nuget.config file on the build server under:
C:\Users\user\AppData\Roaming\NuGet\NuGet.config
In this file I have the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<AzureDevOpsFeed>
<add key="Username" value="MyUsername" />
<add key="PAT" value="MyPatKey" />
</AzureDevOpsFeed>
</configuration>
I would assume the build would use the package source https://api.nuget.org/v3/index.json to restore Automapper.
Any idea why this is not working?
The solution quoated from
NuGet not restoring packages on build
I had to go into Source Control and delete all of the files in the
packages folder (except repositories.config) before NuGet would
restore the missing packages. The idea is that you are using package
restore rather than checking your packages in to source control. If it
sees the packages in source control, it won't download them.
I had same problem. Try to delete nuget packages folder and rebuild the solution.

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>

Azure CI using alternative nuget feed

I'm trying to get Azure to check an alternative nuget feed for packages as well as the default one.
I have configured my CI to auto deploy to an azure webapp when i check into source control.
My solution uses a few nuget packages which are currently only on the Azure nightly feed. I've changed my nuget.targets file as below to include both the nightlies feed and the default nuget feed.
<ItemGroup Condition=" '$(PackageSources)' == '' ">
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
<!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
<PackageSource Include="http://www.myget.org/F/azure-appservice/api/v2/" />
<PackageSource Include="https://api.nuget.org/v3/index.json" />
</ItemGroup>
However when I checkin it fails, and looking at the Activity Logs in Azure I can see that it has failed to find the nightly packages as it's not looking in the the http://www.myget.org/F/azure-appservice/api/v2/ feed
Command: "D:\home\site\deployments\tools\deploy.cmd"
Handling .NET Web Application deployment.
MSBuild auto-detection: using msbuild version '14.0' from 'D:\Program Files (x86)\MSBuild\14.0\bin'.
Feeds used:
C:\DWASFiles\Sites\#1<REDACTED>__43ae\LocalAppData\NuGet\Cache
D:\local\UserProfile\.nuget\packages\
https://api.nuget.org/v3/index.json
Restoring NuGet package Microsoft.Azure.WebJobs.1.1.2-alpha-10245.
Restoring NuGet package Microsoft.Azure.WebJobs.Extensions.1.0.2-alpha-10232.
Restoring NuGet package Microsoft.Azure.WebJobs.Core.1.1.2-alpha-10245.
WARNING: Unable to find version '1.0.2-alpha-10232' of package 'Microsoft.Azure.WebJobs.Extensions'.
WARNING: Unable to find version '1.1.2-alpha-10245' of package 'Microsoft.Azure.WebJobs'.
WARNING: Unable to find version '1.1.2-alpha-10245' of package 'Microsoft.Azure.WebJobs.Core'.
Restoring NuGet package Microsoft.Azure.WebJobs.Extensions.SendGrid.1.0.2-alpha-10232.
WARNING: Unable to find version '1.0.2-alpha-10232' of package 'Microsoft.Azure.WebJobs.Extensions.SendGrid'.
Unable to find version '1.0.2-alpha-10232' of package 'Microsoft.Azure.WebJobs.Extensions.SendGrid'.
Unable to find version '1.1.2-alpha-10245' of package 'Microsoft.Azure.WebJobs.Core'.
Unable to find version '1.1.2-alpha-10245' of package 'Microsoft.Azure.WebJobs'.
Unable to find version '1.0.2-alpha-10232' of package 'Microsoft.Azure.WebJobs.Extensions'.
Failed exitCode=1, command=nuget restore "D:\home\site\repository\<REDACTED>.sln"
An error has occurred during web site deployment.
Unable to find version '1.0.2-alpha-10232' of package 'Microsoft.Azure.WebJobs.Extensions.SendGrid'.\r\nUnable to find version '1.1.2-alpha-10245' of package 'Microsoft.Azure.WebJobs.Core'.\r\nUnable to find version '1.1.2-alpha-10245' of package 'Microsoft.Azure.WebJobs'.\r\nUnable to find version '1.0.2-alpha-10232' of package 'Microsoft.Azure.WebJobs.Extensions'.\r\nC:\Program Files (x86)\SiteExtensions\Kudu\52.50316.2137\bin\scripts\starter.cmd "D:\home\site\deployments\tools\deploy.cmd"
Does anyone know how to configure this?
As per matthews reply https://github.com/Azure/azure-webjobs-sdk/issues/663
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<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.org" value="https://www.nuget.org/api/v2/" />
<add key="azure_app_service" value="https://www.myget.org/F/azure-appservice/api/v2" />
<!--<add key="buildTools" value="https://www.myget.org/F/30de4ee06dd54956a82013fa17a3accb/" />-->
</packageSources>
</configuration>

Categories