配置外部nuget源时恢复项目失败

问题描述 投票:0回答:1

我的机器上的 nuget.config nuget 源配置很少。

其中之一(

externalSource
)查看外部受保护源(以下所有内容均由 VS 生成):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
        <add key="externalSource" value="https://.external_source./nuget/v3/index.json" />
        <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
   </packageSources>
</configuration>

可以在浏览器中从我的机器上的所有源检索json配置,我可以在浏览器中输入正确的凭据(即使用它没有任何问题)。另外,我可以在 VS 中的

Nuget Manager UI
中轻松使用这个 nuget 源代码。

我正在使用的机器也可以访问不同的 nuget 源,但该源未在任何地方配置(在 VS 或任何 nuget.config 中)。

现在,我创建一个简单的控制台应用程序:

PS C:\..> dotnet new console --no-restore
The template "Console App" was created successfully
 

注意:我故意跳过恢复以将问题区域限制为仅

restore
逻辑,如果没有此选项,
new console
命令将失败,即使项目本身将被创建。

现在我想恢复创建的应用程序:

PS C:\..> dotnet restore
  Determining projects to restore...
C:\Program Files\dotnet\sdk\8.0.100\NuGet.targets(156,5): error : Unable to load the service index for source "https://.external_source./nuget/v3/index.json". [C:\..\test.csproj]
C:\Program Files\dotnet\sdk\8.0.100\NuGet.targets(156,5): error :   Response status code does not indicate success: 401 (Unauthorized). [C:\..\test.csproj]

当我启用详细的日志记录详细程度时,我发现此步骤中发生了失败:

 1>Target "Restore" in file "C:\Program Files\dotnet\sdk\8.0.100\NuGet.targets" from project "C:\..\test.csproj" (entry point):
   Task "RemoveDuplicates"
   Done executing task "RemoveDuplicates".
   Using "RestoreTask" task from assembly "C:\Program Files\dotnet\sdk\8.0.100\NuGet.Build.Tasks.dll".
   Task "RestoreTask"
     (in) RestoreGraphItems Count '4'
     (in) RestoreDisableParallel 'False'
     (in) RestoreNoCache 'False'
     (in) RestoreIgnoreFailedSources 'False'
     (in) RestoreRecursive 'True'
     (in) RestoreForce 'False'
     (in) HideWarningsAndErrors 'False'
     (in) RestoreForceEvaluate 'False'
     (in) RestorePackagesConfig 'False'
..

   CACHE https://api.nuget.org/v3/vulnerabilities/vulnerability.update.json
     1>C:\Program Files\dotnet\sdk\8.0.100\NuGet.targets(156,5): error : Unable to load the service index for source https://.external_source./nuget/v3/index.json. [C:\..\test.csproj]
       C:\Program Files\dotnet\sdk\8.0.100\NuGet.targets(156,5): error :   Response status code does not indicate success: 401 (Unauthorized). [C:\..\test.csproj]
         Assembly loaded during TaskRun (NuGet.Build.Tasks.RestoreTask): System.Diagnostics.StackTrace, Version=8.0.0.0, Culture=neutral, PublicKeyToken=.. (location: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0\System.Diagnostics.StackTrace.dll, MVID: .., AppDomain: [Default])
         NuGet.Protocol.Core.Types.FatalProtocolException: Unable to load the service index for source https://.external_source./nuget/v3/index.json.
          ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
..

我该如何修复它?

配置如下:

  • 在 nuget.config 中:

    <packageSourceCredentials>    
       <externalSource>      
          <add key="Username" value="name" />      
          <add key="ClearTextPassword" value=".." />    
       </externalSource>  
    </packageSourceCredentials>
    
  • dotnet restore
    命令的选项:
    --interactive
    --ignore-failed-sources
    没有帮助。我可以使用选项
    --source=nuget.org
    修复它,但我必须配置
    externalSource
    ,因为在其他一些情况下应该从这个源进行恢复

更新:如果我通过以下方式检索nugets,错误就会消失:

nuget search -take 10 -Source externalSource
(当我运行它时,我没有看到任何提示或弹出窗口)在错误发生后,但只持续大约30分钟,然后它开始失败并显示401又出错了

UPDATE2 可重现问题的机器是 Windows 11 Enterprise,不确定这是否重要,但我在 Windows 10 上没有看到任何问题

UPDATE3 看起来解决问题的另一种方法是调用

msbuild -t:Restore
。这听起来像是 dotnet 二进制问题吗?

c# .net nuget nuget-package nuget-package-restore
1个回答
0
投票

正确的答案确实是下载并设置artifacts凭证提供程序

在 Azure DevOps 工件上设置源时,系统会向您提供从多个源连接到源的说明。如果我们选择 dotnet CLI 的说明,则会显示以下内容:


© www.soinside.com 2019 - 2024. All rights reserved.