简体   繁体   English

Nuget - 对象引用未设置为对象的实例

[英]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.我在 vs 2015 中遇到了 nuget 包管理器的问题。像 Unity 这样的一些包已经安装没有问题。 Some packages like EF had problems while installing.某些软件包(例如 EF)在安装时出现问题。 Some packages like Automapper had the same problem but when I installed another version of this package, It installed fine.一些像 Automapper 这样的包也有同样的问题,但是当我安装这个包的另一个版本时,它安装得很好。

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.使用 Visual Studio 2015 中使用的新协议(版本 3)nuget 是一个常见问题。它与多个 nuget 包一起使用,例如 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:并编辑 XML 配置文件,注释协议 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.版本 2 的维护设置。保存并重新启动 Visual Studio 2015。您现在应该毫无问题地恢复 nuget 包。

Same problem still exists in Visual Studio 2022. The solution:在 Visual Studio 2022 中仍然存在同样的问题。解决方法:

  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导航到 C:\Users<您的用户名>\AppData\Roaming\NuGet // 记住 AppData 是一个隐藏文件夹,因此您必须让 Windows 显示隐藏文件和文件夹才能看到它
  2. Open NuGet.Config in Notepad (or whatever will let you edit it)在记事本中打开 NuGet.Config(或任何可以让您编辑它的东西)
  3. In the "packageSources" section, look for the line:在“packageSources”部分中,查找以下行:

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

and delete 'protocolVersion="3"'并删除 'protocolVersion="3"'

  1. Save the file and restart Visual Studio 2022保存文件并重新启动 Visual Studio 2022

When i've had Nuget problems in the past these are the steps i've done:当我过去遇到 Nuget 问题时,这些是我已经完成的步骤:

1) Use Nuget package manager to remove out of project(s) 1) 使用 Nuget 包管理器从项目中删除

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) 2) Clean and Build (你会有错误,因为你不再有引用 ​​2) 确保从你的 packages.config 中删除包(如果没有,删除并重复步骤 2)

3) Make sure the .dlls were removed from your bin folder (if not, delete and repeat step 2) 3) 确保 .dll 已从您的 bin 文件夹中删除(如果没有,请删除并重复步骤 2)

4) Add packages back in and rebuild. 4)重新添加包并重建。 Ensure the packages were added to packages.config (with appropriate version) and .dlls were added back to the bin folder (with appropriate version).确保将包添加到 packages.config(具有适当的版本),并将 .dlls 添加回 bin 文件夹(具有适当的版本)。

I am running VS 2017, but was having this error with the TFS build server during the "dotnet restore" task.我正在运行 VS 2017,但在“dotnet restore”任务期间 TFS 构建服务器出现此错误。 The issue was we had an internal local file share, but the nuget.config had protocolVersion="3" for that entry.问题是我们有一个内部本地文件共享,但 nuget.config 该条目的 protocolVersion="3"。 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 )我的问题发生在NuGet.Config (他NuGet.Config文件路径: C:\Users\<UserName>\AppData\Roaming\NuGet

  2. Reason: Tools -> NuGet Package Manager -> Package Management Settings -> Package Source ->原因:工具-> NuGet 包管理器->包管理设置->包源->

     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:默认包源有protocolVersion="3"属性,但我修改为我的离线包源,同时新建一个包源:

     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.新的包源没有属性protocolVersion="3" ,所以无论是在线还是离线我总是无法安装包。

  3. Solution: Add ProtocalVersion="3" to解决方案:将ProtocalVersion="3"添加到

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

    and remove ProtocalVersion="3" in my offline package source.并在我的离线包源中删除ProtocalVersion="3"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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