简体   繁体   中英

Nuget - Object reference not set to an instance of an object

I have a problem with nuget package manager in vs 2015. Some packages like Unity has been installed with no problem. Some packages like EF had problems while installing. Some packages like Automapper had the same problem but when I installed another version of this package, It installed fine.

here is the output:

System.NullReferenceException: Object reference not set to an instance of an object.


at NuGet.Protocol.Core.v3.GlobalPackagesFolderUtility.<AddPackageAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at NuGet.Protocol.Core.v3.DownloadResourceV3.<GetDownloadResourceResultAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.PackageDownloader.<GetDownloadResourceResultAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__42.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__42.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.UI.UIActionEngine.<ExecuteActionsAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.UI.UIActionEngine.<PerformActionAsync>d__3.MoveNext()
========== Finished ==========

It is a common problem with the use of the new protocol (version 3) nuget used in Visual Studio 2015. It is with multiple nuget packages, for example Microsoft.Owin 3.0.1. To fix this go to:

C:\Users\<your user name>\AppData\Roaming\NuGet

And edit the XML configuration file, commenting the line of protocol 3:

<!-- add key = "nuget.org" value = "https://api.nuget.org/v3/index.json" protocolVersion = "3" /-->
<add key = "nuget.org" value = "https://www.nuget.org/api/v2/" /> 

Maintenance settings for version 2. Save and restart visual studio 2015. You should now restore the nuget packages without problems.

Same problem still exists in Visual Studio 2022. The solution:

  1. Navigate to C:\Users<your user name>\AppData\Roaming\NuGet // remember AppData is a hidden folder, so you will have to make Windows show hidden files and folders before you can see it
  2. Open NuGet.Config in Notepad (or whatever will let you edit it)
  3. In the "packageSources" section, look for the line:

add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3"

and delete 'protocolVersion="3"'

  1. Save the file and restart Visual Studio 2022

When i've had Nuget problems in the past these are the steps i've done:

1) Use Nuget package manager to remove out of project(s)

2) Clean and Build (you'll have errors because you do longer have the references 2) Make sure the packages were removed from your packages.config (if not, delete and repeat step 2)

3) Make sure the .dlls were removed from your bin folder (if not, delete and repeat step 2)

4) Add packages back in and rebuild. Ensure the packages were added to packages.config (with appropriate version) and .dlls were added back to the bin folder (with appropriate version).

I am running VS 2017, but was having this error with the TFS build server during the "dotnet restore" task. The issue was we had an internal local file share, but the nuget.config had protocolVersion="3" for that entry. After removing that property, the build was successful.

  1. My problem happens in NuGet.Config (he NuGet.Config file path: C:\Users\<UserName>\AppData\Roaming\NuGet )

  2. Reason: Tools -> NuGet Package Manager -> Package Management Settings -> Package Source ->

     nuget.org=https://api.nuget.org/v3/index.json

    The default package source has attribute protocolVersion="3" ,but I modified it to my offline package source, meanwhile creating a new package source:

     nuget.ort=https://api.nuget.org/v3/index.json

    The new package source has no attribute protocolVersion="3" ,so I always fail to install packages whether of online or offline.

  3. Solution: Add ProtocalVersion="3" to

    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3"/>

    and remove ProtocalVersion="3" in my offline package source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM