【问题标题】:How do I restore a NuGet package using the CLI?如何使用 CLI 还原 NuGet 包?
【发布时间】:2021-03-20 07:26:12
【问题描述】:

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

[Solution file] 是我的 .sln 文件的路径

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

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

在同一台机器上,我可以在 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 并将详细程度设置为详细并得到以下结果:

NuGet Version: 5.8.0.6930
MSBuild auto-detection: using msbuild version '15.9.21.664' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.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 Studio\2017\BuildTools\MSBuild\15.0\Bin\msbuild.exe "C:\Users\buildadmin\AppData\Local\Temp\NuGetScratch\0bsxh5zl.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\0.2.29\node_modules\nuget-task-common\NuGet\5.8.0\nuget.exe" /p:RestoreSolutionDirectory="E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\40\s\HANDHELD\Android\2.0\2.0.9\\" /p:RestoreConfigFile="E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\40\s\HANDHELD\Android\2.0\2.0.9\.nuget\NuGet.Config" /p:SolutionDir="E:\TFSBuilds\Agents\AFSDSDHandheld_Agent\_work\40\s\HANDHELD\Android\2.0\2.0.9\\" /p:SolutionName="DsdMobile"
C:\Program Files (x86)\Microsoft Visual Studio\2017\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\40\s\HANDHELD\Android\2.0\2.0.9\Ross.Core.Dex\Ross.Core.Dex.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\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\40\s\HANDHELD\Android\2.0\2.0.9\Ross.Core.Dex\Ross.Core.Dex.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\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\40\s\HANDHELD\Android\2.0\2.0.9\Ross.Localization\Ross.Localization.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\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\40\s\HANDHELD\Android\2.0\2.0.9\Ross.Localization\Ross.Localization.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\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\40\s\HANDHELD\Android\2.0\2.0.9\Ross.UI\Ross.UI.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\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\40\s\HANDHELD\Android\2.0\2.0.9\Ross.UI\Ross.UI.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\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\40\s\HANDHELD\Android\2.0\2.0.9\RossServices\RossServices.csproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\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\40\s\HANDHELD\Android\2.0\2.0.9\RossServices\RossServices.csproj]

Restoring NuGet package Plugin.Fingerprint.1.4.9.
Missing C:\Users\buildadmin\.nuget\packages\plugin.fingerprint\1.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\40\s\HANDHELD\Android\2.0\2.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文件是怎么写的?你能和我们分享一下吗?问题与该文件有关。
  • @Perry Qian-MSFT 更新了我的帖子以包含 nuget.config 文件的内容
  • 我认为您的nuget.exe 太旧了。当我使用nuget.exev2.8.6,v 3.3.0 时,就会发生错误。实际上,我用最新的v5.8.0 测试了你的nuget.config,没有错误。所以我认为你的nuget.config 文件没有问题。问题可能出在您的 nuget.exe 版本上。
  • 所以我认为你应该将你的 nuget.exe 更新到最新版本。所以请从this link下载使用最新的nuget.exev5.8.0
  • 我也更新了答案。

标签: visual-studio nuget


【解决方案1】:

首先,您应该仔细检查您的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,就可以正常使用了。

一开始,我在&lt;config&gt;&lt;/config&gt; 下写了任何其他节点,这会导致与您所说的相同的错误:

错误的:

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

之后,我发现&lt;config&gt;&lt;/config&gt;&lt;packageSources&gt;&lt;/packageSources&gt;的等级是一样的。

您应该关注this official document 来编写您的nuget.config 文件。

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

更新 1

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

更新 2

通过进一步调查,问题更加清晰。

发送请求时出错。请求被中止: 无法创建 SSL/TLS 安全通道。

您的 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 源。

【讨论】:

  • 我没有安装 TLS 1.3,所以我跳过了 #1 而只是做了 #2 并修复了它!非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多