使用 dotnet add 包忽略无法访问的源

spo*_*ahn 6 c# command-line-interface nuget .net-core

当我尝试通过 cli 添加 NuGet 包时,dotnet出现错误,指出它无法访问我的自定义 NuGet 源之一。有没有办法说“我不在乎,从你可以恢复的地方恢复”?

\n\n

McMaster.Extensions.CommandLineUtils显然它存在于 NuGet.org 中,并且找到它,但随后停止,因为它无法访问自定义源 \xe2\x80\x8d\xe2\x99\x82\xef\xb8\x8f。

\n\n
\n

PS c:\\Temp\\blah-project> dotnet add package McMaster.Extensions.CommandLineUtils
\n info : 将包 \'McMaster.Extensions.CommandLineUtils\' 的 PackageReference 添加到项目 \'c:\\Temp\\blah-项目\\blah-project.csproj\'。
\n 信息:恢复 c:\\Temp\\blah-project\\blah-project.csproj 的包
\n 信息:GET https://api.nuget.org/v3-flatcontainer/mcmaster.extensions.commandlineutils/index .json
\n 信息:确定https://api.nuget.org/v3-flatcontainer/mcmaster.extensions.commandlineutils/index.json 147ms
\n 错误:无法加载源https://myinstace.pkgs的服务索引.visualstudio.com/_packaging/Blah/nuget/v3/index.json
\n 错误:响应状态代码不表示成功:401(未经授权)。

\n
\n

Chr*_*ris 4

dotnet命令具有指定的选项--source。这允许您仅从特定位置恢复包,在这种情况下您需要使用

dotnet restore --source https://api.nuget.org/v3/index.json
Run Code Online (Sandbox Code Playgroud)

这应该将包下载到本地 NuGet 存储并解决您计算机上的问题

为了将包添加到您的项目中,您可能需要手动添加<PackageReference>到您的项目中,例如

<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.6.0" />
Run Code Online (Sandbox Code Playgroud)

然后运行上面的恢复命令将为您获取该包。

作为更永久的修复,您应该将一个nuget.config文件放在项目/存储库的根目录中,在其中指定该特定项目的包源。您可以设置一些设置来覆盖系统的全局 nuget 配置/源。请参阅有关nuget.config的更多信息。您可以在存储库中创建一个起始 nuget.configdotnet new nugetconfig