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

6

我在VS 2015中使用Nuget包管理器遇到了问题。一些包,例如Unity,安装没有问题。但是有些包,如EF,在安装时出现问题。另外一些包,例如Automapper,也遇到了相同的问题,但是当我安装了该包的另一个版本时,安装成功了。

以下是输出:

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 ==========

请提供更多的描述信息 - user5166622
我尝试简要地表达。但是我应该说什么描述呢? - m.ghadiri
请提供更多关于问题的信息,您所涉及的项目类型等等。 - user5166622
1
问题在于NuGet无法正常工作。有时候某些软件包没有问题,有时候安装某些软件包会出现问题,但是没有任何原因!唯一的错误就是我上面提到的那个。但我认为这不是VS或项目类型的问题,因为我在VS之外使用NuGet控制台时也遇到了同样的问题。 - m.ghadiri
5个回答

18

在使用Visual Studio 2015中的新协议(版本3)NuGet时,多个NuGet包存在共同问题,例如Microsoft.Owin 3.0.1。解决此问题,请转到:

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

同时编辑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/" /> 

版本2的维护设置。保存并重新启动Visual Studio 2015。现在,您应该可以顺利地恢复NuGet包。


12

在 Visual Studio 2022 中仍存在相同的问题。解决方法:

  1. 导航到 C:\Users\<你的用户名>\AppData\Roaming\NuGet // 注意 AppData 是一个隐藏文件夹,所以你需要在 Windows 中显示隐藏的文件和文件夹才能看到它
  2. 用记事本(或其他可以编辑它的软件)打开 NuGet.Config 文件
  3. 在 "packageSources" 部分,查找以下内容:

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

并删除 'protocolVersion="3"'

  1. 保存文件并重新启动 Visual Studio 2022

1
太好了,谢谢@Will。我已经习惯了NuGet的顺畅运行,所以当出现这样的情况时感到很震惊。 - Paul McCarthy

1
当我遇到Nuget问题时,我通常会执行以下步骤:
1)使用Nuget包管理器从项目中删除
2)清理并构建(您会遇到错误,因为您不再拥有引用)
3)确保从packages.config中删除了这些包(如果没有,请删除并重复步骤2)
4)确保从bin文件夹中删除了.dll文件(如果没有,请删除并重复步骤2)
5)重新添加包并重新构建。 确保已将包添加到packages.config(具有适当的版本),并且.dll文件已以适当的版本添加回bin文件夹。

问题是我从未安装它们,也没有packages.config文件,也没有该软件包的另一个程序集!我的解决方案中有7个项目,每个项目中都有不同的软件包。 - m.ghadiri
为了避免问题4),您可以使用项目依赖浏览器(http://projectdependencybrowser.org),该工具可以分析C#项目中的NuGet/DLL引用问题... - Rico Suter

0
  1. 我的问题出现在 NuGet.Config 文件中(文件路径:C:\Users\<UserName>\AppData\Roaming\NuGet)。

  2. 原因:在 工具 -> NuGet 包管理器 -> 包管理设置 -> 包源 中,将默认的包源属性 protocolVersion="3" 修改为了我自己的离线包源,并创建了一个新的包源:

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

    新的包源没有属性 protocolVersion="3",所以我无论是在线还是离线安装包都失败了。

  3. 解决方案:在

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

    中添加 ProtocolVersion="3",并从我的离线包源中删除 ProtocolVersion="3"


0

我正在运行VS 2017,但在TFS构建服务器的“dotnet restore”任务期间遇到了这个错误。问题是我们有一个内部本地文件共享,但nuget.config对该条目设置了protocolVersion =“3”。删除该属性后,构建成功。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接