首页 > 解决方案 > 无法安装 NUnit NuGet 包,但其他 NuGet 包安装正常

问题描述

我有一个 Visual Studio 解决方案,并试图通过 NuGet 在解决方案的单元测试项目中安装 NUnit。即使主应用程序项目的其他 NuGet 包安装正常,我也无法安装它。

最终,我决定使用单个 .NET Core 类库项目创建另一个测试解决方案。除了创建项目时自动包含的空类 Class1 之外,该项目是空的。使用“Manage NuGet Packages for Solution...”窗口,我尝试安装两个包,Newtonsoft.Json 和 NUnit。安装了 Newtonsoft.Json,NUnit 没有。

这是我安装 Newtonsoft.Json 时的输出:

Restoring packages for C:\Temp\Test\Test\Test.csproj...
Installing NuGet package Newtonsoft.Json 12.0.3.
Committing restore...
Writing assets file to disk. Path: C:\Temp\Test\Test\obj\project.assets.json
Successfully installed 'Newtonsoft.Json 12.0.3' to Test
Executing nuget actions took 718.62 ms
Time Elapsed: 00:00:00.7711055
========== Finished ==========

Time Elapsed: 00:00:00.0382855
========== Finished ==========

这是我尝试安装 NUnit 时非常不同的输出:

Restoring packages for C:\Temp\Test\Test\Test.csproj...
  GET https://www.nuget.org/api/v2/FindPackagesById()?id='NUnit'&semVerLevel=2.0.0
  OK https://www.nuget.org/api/v2/FindPackagesById()?id='NUnit'&semVerLevel=2.0.0 1028ms
  GET https://www.nuget.org/api/v2/package/NUnit/3.12.0
  GET https://www.nuget.org/api/v2/package/NUnit/3.12.0
  GET https://www.nuget.org/api/v2/package/NUnit/3.12.0
  GET https://www.nuget.org/api/v2/FindPackagesById()?id='NUnit'&semVerLevel=2.0.0
  OK https://www.nuget.org/api/v2/FindPackagesById()?id='NUnit'&semVerLevel=2.0.0 273ms
  CACHE https://www.nuget.org/api/v2/FindPackagesById()?id='NUnit'&semVerLevel=2.0.0
  CACHE https://www.nuget.org/api/v2/FindPackagesById()?id='NUnit'&semVerLevel=2.0.0
The feed 'nuget.org [https://www.nuget.org/api/v2/]' lists package 'NUnit.3.12.0' but multiple attempts to download the nupkg have failed. The feed is either invalid or required packages were removed while the current operation was in progress. Verify the package exists on the feed and try again.
  Unable to find package 'NUnit.3.12.0'.
Time Elapsed: 00:00:04.7743652
========== Finished ==========

这是使用 Visual Studio 2019(版本 16.4.3)。项目目标框架为 .NET Core 3.1,输出类型为类库。

我注意到 NuGet.org 上的 NUnit 页面说它支持 .NET Core。我还可以通过浏览到上面输出中给出的 URL https://www.nuget.org/api/v2/package/NUnit/3.12.0手动下载 NUnit 包

当我可以将 Newtonsoft.Json 安装到同一个项目中时,有谁知道为什么我不能安装 NUnit?

标签: visual-studio.net-corenugetnunit

解决方案


当我可以将 Newtonsoft.Json 安装到同一个项目中时,有谁知道为什么我不能安装 NUnit?

这是一种异常行为,当我在 Net Core 3.1 ClassLibrary 中安装两个 nuget 包Newtonsoft.JsonNUnit,它运行良好并且没有显示任何错误。

因此,请尝试以下步骤来解决您的问题:

解决方案

1)通过命令行清理所有NuGet包缓存:nuget locals all -clear.

2)关闭VS Instance,删除下的NuGet.Config文件C:\Users\xxx(User Name)\AppData\Roaming\NuGet\NuGet.Config,重启VS,然后恢复nuget包。

3)检查是否有防火墙策略或其他代理设置阻止nuget安装包。

希望它可以帮助你。


推荐阅读