如何使用 CLI 恢复 NuGet 包?

How do I restore a NuGet package using the CLI?

我正在尝试使用 CLI 在我的项目中恢复 NuGet 包。我正在使用的命令如下所示: NuGet.exe 恢复 [解决方案文件] -ConfigFile [配置文件] -NoCache

[解决方案文件] 是我的 .sln 文件的路径

[Config file] 是我解决方案的.nuget 文件夹中 NuGet.Config 文件的路径

当我 运行 这个命令时,我总是得到“无法找到包的 版本。”任何更新的包都会发生这种情况。

在同一台机器上,我可以在 Visual Studio 中打开解决方案,它会很好地更新包。 Visual Studio 设置为使用来源:https://api.nuget.org/v3/index.json,这是报告为从 CLI 使用的相同提要。无法弄清楚为什么它可以从 VS 而不是 CLI。

更新: 我原来的 nuget.config 看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <solution>
        <add key="disableSourceControlIntegration" value="true" />
      </solution>
      <packageSources>
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
      </packageSources>
    </configuration>

我根据Perry的回答修改成这个。我用更新的配置得到了相同的结果:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <config>
    
        </config>
        <packageSources>
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
        </packageSources>
        <packageRestore>
           <add key="enabled" value="True" />
           <add key="automatic" value="True" />
        </packageRestore>
        <bindingRedirects>
           <add key="skip" value="False" />
        </bindingRedirects>
        <packageManagement>
           <add key="format" value="0" />
          <add key="disabled" value="True" />
        </packageManagement>
    </configuration>

已将 NuGet.exe 更新到版本 5.8.0 并将 Verbosity 设置为 Detailed 并得到以下结果:

NuGet Version: 5.8.0.6930
MSBuild auto-detection: using msbuild version '15.9.21.664' from 'C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin'. Use option -MSBuildVersion to force nuget to use a specific version of MSBuild.
MSBuild P2P timeout [ms]: 120000
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\msbuild.exe "C:\Users\buildadmin\AppData\Local\Temp\NuGetScratch[=12=]bsxh5zl.jlh.nugetinputs.targets" /t:GenerateRestoreGraphFile /nologo /nr:false /v:q /p:NuGetRestoreTargets="C:\Users\buildadmin\AppData\Local\Temp\NuGetScratch\wc050pok.qyy.nugetrestore.targets" /p:RestoreUseCustomAfterTargets="True" /p:RestoreTaskAssemblyFile="E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\_tasks\NuGetInstaller_333b11bd-d341-40d9-afcf-b32d5ce6f23b[=12=].2.29\node_modules\nuget-task-common\NuGet.8.0\nuget.exe" /p:RestoreSolutionDirectory="E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\" /p:RestoreConfigFile="E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\.nuget\NuGet.Config" /p:SolutionDir="E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\" /p:SolutionName="DsdMobile"
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(725,2): warning XA0113: Google Play requires that new applications must use a TargetFrameworkVersion of v8.0 (API level 26) or above. You are currently targeting v5.0 (API level 21). [E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\Ross.Core.Dex\Ross.Core.Dex.csproj]
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(725,2): warning XA0114: Google Play requires that application updates must use a TargetFrameworkVersion of v8.0 (API level 26) or above. You are currently targeting v5.0 (API level 21). [E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\Ross.Core.Dex\Ross.Core.Dex.csproj]
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(725,2): warning XA0113: Google Play requires that new applications must use a TargetFrameworkVersion of v8.0 (API level 26) or above. You are currently targeting v5.0 (API level 21). [E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\Ross.Localization\Ross.Localization.csproj]
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(725,2): warning XA0114: Google Play requires that application updates must use a TargetFrameworkVersion of v8.0 (API level 26) or above. You are currently targeting v5.0 (API level 21). [E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\Ross.Localization\Ross.Localization.csproj]
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(725,2): warning XA0113: Google Play requires that new applications must use a TargetFrameworkVersion of v8.0 (API level 26) or above. You are currently targeting v5.0 (API level 21). [E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\Ross.UI\Ross.UI.csproj]
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(725,2): warning XA0114: Google Play requires that application updates must use a TargetFrameworkVersion of v8.0 (API level 26) or above. You are currently targeting v5.0 (API level 21). [E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\Ross.UI\Ross.UI.csproj]
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(725,2): warning XA0113: Google Play requires that new applications must use a TargetFrameworkVersion of v8.0 (API level 26) or above. You are currently targeting v5.0 (API level 21). [E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\RossServices\RossServices.csproj]
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(725,2): warning XA0114: Google Play requires that application updates must use a TargetFrameworkVersion of v8.0 (API level 26) or above. You are currently targeting v5.0 (API level 21). [E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\RossServices\RossServices.csproj]

Restoring NuGet package Plugin.Fingerprint.1.4.9.
Missing C:\Users\buildadmin\.nuget\packages\plugin.fingerprint.4.9\plugin.fingerprint.1.4.9.nupkg
WARNING: Unable to find version '1.4.9' of package 'Plugin.Fingerprint'.
  C:\Users\buildadmin\.nuget\packages\: Package 'Plugin.Fingerprint.1.4.9' is not found on source 'C:\Users\buildadmin\.nuget\packages\'.
  https://api.nuget.org/v3/index.json: Unable to load the service index for source https://api.nuget.org/v3/index.json.
  An error occurred while sending the request.
  The request was aborted: Could not create SSL/TLS secure channel.

WARNING: Unable to find version '1.4.9' of package 'Plugin.Fingerprint'.
  C:\Users\buildadmin\.nuget\packages\: Package 'Plugin.Fingerprint.1.4.9' is not found on source 'C:\Users\buildadmin\.nuget\packages\'.
  https://api.nuget.org/v3/index.json: Unable to load the service index for source https://api.nuget.org/v3/index.json.
  An error occurred while sending the request.
  The request was aborted: Could not create SSL/TLS secure channel.

NuGet Config files used:
    E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\s\HANDHELD\Android.0.0.9\.nuget\NuGet.Config

Feeds used:
    C:\Users\buildadmin\.nuget\packages\
    https://api.nuget.org/v3/index.json

我使用的是 VS 2019,所以我不确定是否需要指定它。我假设问题与“无法创建 SSL/TLS 安全通道”消息有关,但我不确定如何解决该问题。

首先,您应该仔细检查您的 nuget.config 文件。

这是 nuget.config 文件的正确格式:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    
    <config>

    </config>
    <packageSources>
            <add key="public feed" value="https://api.nuget.org/v3/index.json"/>
    </packageSources>
    <packageRestore>
       <add key="enabled" value="True" />
       <add key="automatic" value="True" />
   </packageRestore>
   <bindingRedirects>
       <add key="skip" value="False" />
   </bindingRedirects>
   <packageManagement>
       <add key="format" value="0" />
      <add key="disabled" value="True" />
  </packageManagement>

</configuration>

然后,使用命令nuget restore xxx\ConsoleApp38.sln -ConfigFile "xxx\ConsoleApp38\.nuget\nuget.config" -NoCache,就可以了

一开始,我在<config></config>下写了任何其他节点,这会导致与您所说的相同的错误,如下所示:

错误的:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <config>

        <packageSources>
            <add key="public feed" value="https://api.nuget.org/v3/index.json"/>
        </packageSources>

    </config>
</configuration>

之后,我发现<config></config><packageSources></packageSources>的水平一样。

您应该按照 this official document 编写您的 nuget.config 文件。

此外,如果您没有找到您的nuget.config文件,您可以与我们分享,让我们找出问题所在。另外,您可以使用*替换隐私信息。

更新 1

还有,因为你的nuget.exe太旧了,旧的可能会有一些问题。您应该使用我已经成功测试过的最新 nuget.exe v5.8.0。所以请从 this link.

下载

更新 2

随着调查的深入,问题更加明确。

An error occurred while sending the request. The request was aborted: Could not create SSL/TLS secure channel.

您的 Internet 或网络协议可能有一些问题。如果您使用任何代理,请禁用它。确保您的互联网是纯净的。

此外,如果这没有帮助,请遵循建议:

建议

1) 以管理员身份打开 powershell,运行 这些:

New-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server' -Force | Out-Null
    
New-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server' -name 'Enabled' -value '0' -PropertyType 'DWord' -Force | Out-Null
    
New-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server' -name 'DisabledByDefault' -value 1 -PropertyType 'DWord' -Force | Out-Null
    
New-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Client' -Force | Out-Null
    
New-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Client' -name 'Enabled' -value '0' -PropertyType 'DWord' -Force | Out-Null
    
New-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Client' -name 'DisabledByDefault' -value 1 -PropertyType 'DWord' -Force | Out-Null
Write-Host 'TLS 1.3 has been disabled.'

reg add HKLM\SOFTWARE\Microsoft\.NETFramework\v4.0.30319 /v SystemDefaultTlsVersions /t REG_DWORD /d 1 /f /reg:64
reg add HKLM\SOFTWARE\Microsoft\.NETFramework\v4.0.30319 /v SystemDefaultTlsVersions /t REG_DWORD /d 1 /f /reg:32

2) 使用它打开 TSL 1.2

reg add "HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" /v DisabledByDefault /t REG_DWORD /d 0 /f /reg:32
reg add "HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" /v DisabledByDefault /t REG_DWORD /d 0 /f /reg:64
reg add "HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" /v Enabled /t REG_DWORD /d 1 /f /reg:32
reg add "HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" /v Enabled /t REG_DWORD /d 1 /f /reg:64

3) 另外,你可以尝试使用 http://packages.nuget.org/v1/FeedService.svc/ 作为 nuget.org 来源。