【问题标题】:how to resolve "Unable to find package" nuget error如何解决“无法找到包”nuget错误
【发布时间】:2022-04-01 22:28:26
【问题描述】:

正在为依赖于作为项目参考添加的另一个项目 (B.csprojec) 的项目 (A.csproj) 创建 nuget pkg。
这是 .nuspec ,

<?xml version="1.0"?>
<package >
  <metadata>
    <id>A.Client</id>
    <title>A.Client</title>
    <description>HttpClient and Models for calling the A Microservice.</description>
    <version>1.0.2</version>
    <authors></authors>
    <owners></owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <copyright>.</copyright>
    <tags></tags>
    <dependencies>
      <group targetFramework=".NETFramework4.5">
        <dependency id="Newtonsoft.Json" version="9.0.1" exclude="Build,Analyzers" />
        // <dependency id="B" version="1.0.0"/> tried this but same error
      </group>
      <group targetFramework=".NETStandard2.0">
        <dependency id="Newtonsoft.Json" version="9.0.1" exclude="Build,Analyzers" />
        // <dependency id="B" version="1.0.0"/> tried this but same error
      </group>
    </dependencies>
    <frameworkAssemblies>
      <frameworkAssembly assemblyName="System.Net.Http" targetFramework=".NETFramework4.5" />
    </frameworkAssemblies>
  </metadata>
  <files>
    <file src="bin\$configuration$\netstandard2.0\A.dll" target="lib\netstandard2.0" />
    <file src="bin\$configuration$\net45\A.dll" target="lib\net45" />
  </files>
</package>

我用过

nuget pack A.nuspec -Properties configuration=debug

生成包。但是,当我尝试在 c.csprojc 中使用此包时,出现以下错误

Severity    Code    Description Project File    Line    Suppression State
Error   NU1101  Unable to find B. No packages exist with this id in source(s): Local Package source, Microsoft Visual Studio Offline Packages, nuget.org, Package source    

我错过了什么?

【问题讨论】:

  • 您是否构建了包 B?该错误表明包 B 未构建或无法从包含包 A 的目录中获得。您使用的 nuget pack 命令仅提及 A.nuspec。
  • a 和 b.csproj 在同一个解决方案中,a 引用 b 作为项目,而不是包。
  • 您在使用nuget pack 时尝试过-IncludeReferencedProjects 选项吗?
  • 在Tools -> Nuget package Manager -> package manager settings (Package source tab)下设置包源地址:api.nuget.org/v3/index.json

标签: nuget nuspec


【解决方案1】:

包源离线!!

我在我的系统上安装了VS2017专业版,打开一个现有项目,发现缺少多个包。我尽我所能,没有看包源!!

我正在写这个答案,因为我尝试了以下解决方案,但它们都没有奏效:

  1. 清除缓存
  2. 恢复或重新安装软件包
  3. 更改目标框架
  4. 更新 Nuget 包管理器

解决方案

步骤 01. 转到 包管理器设置(工具 > Nuget 包管理器 > 包管理器设置)

步骤 02。检查包源。可以看到,包源在这里已经下载了SDK/nugets/packages。我不知道原因,但我的 Visual Studio 系统安装中缺少来自 nuget.org 的在线包源。

步骤 03. 安装 nuget.org 作为包源,然后“清除所有 Nuget 缓存”,然后恢复包。错误会消失。

名称:nuget.org(或如您所愿) 来源:https://api.nuget.org/v3/index.json

【讨论】:

  • 这正是我的解决方案。我不知道为什么 VS 安装程序会排除 nuget.org url 作为包源,你会认为它会被包含为默认源。
  • 谢谢。这对我来说是个问题,你为我指出了一个确切的解决方案,我苦了几天才找到这个页面。
  • 谢谢,谢谢。几年后我才回到 Visual Studio,遇到了一些错误。找到了一些我无法理解的答案,或者这似乎是不好的解决方法。找到了你的,这很有意义并且有效!现在我可以真正开始编码了!
  • 对于在 Azure 构建中遇到此问题的任何人,此打开的 Azure DevOps 问题 #9046 提供了一个答案:github.com/NuGet/Home/issues/9046#issuecomment-579012605 简而言之,在您的项目中添加一个 nuget.config 文件,并在您之前添加 &lt;clear/&gt;包源强制构建过程再次下载它,防止出现这种“离线包”错误。
  • 宾果游戏。由于奇怪地没有设置 nuget 源,这对于 MS 团队来说确实是不可接受的。我希望这只是一个错误,但它是 2021 年,我正在评论 2019 年的 cmets。作为一个有经验的.NET开发者,在需要重装系统的时候,完全安装了VStudio 2019 Pro最新版本,手动安装了.NET Framework 4.8和dotnet core 5.0及3版本(dot.net),但还是有这个问题?!这怎么可能?令人难以置信的是,他们以某种方式默认具有此设置。很抱歉抱怨,但这不利于收养。
【解决方案2】:

我刚做了这个,一切顺利。在您的 Visual Studio 中,转到:

  1. 工具
  2. Nuget 包管理器
  3. 常规
  4. 单击“清除所有 Nuget 缓存”按钮
  5. 打包资源并点击“更新”
  6. 哇!错误消失了...

【讨论】:

    【解决方案3】:

    GitHub 操作

    我想补充一点,在 Github Actions 中,默认情况下不查询 nuget.org 存储库。相反,GitHub 默认使用缓存的包存储库,并且您引用的包可能不在该缓存中,而是在 nuget.org 上(我为 System.CommandLine 提供了它)。

    您可以通过将nuget.config 文件添加到包含以下内容的根目录来解决此问题

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

    github_action.yml 文件示例

    name: .NET
    
    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master ]
    
    jobs:
      build:
    
        runs-on: windows-latest
    
        steps:
        - uses: actions/checkout@v2
        - name: Setup .NET
          uses: actions/setup-dotnet@v1
          with:
            dotnet-version: 5.0
        - name: Clean
          run: dotnet clean && dotnet nuget locals all --clear
        - name: Restore dependencies
          run: dotnet restore
        - name: Build
          run: dotnet build --no-restore
        - name: Test
          run: dotnet test --no-build --verbosity normal
    

    【讨论】:

    • 这让我难过一阵子,因为我有一个 ubuntu-latestwindows-latest 的矩阵构建,而 Linux 的构建在 NuGet 上运行得非常好,但在 Windows 上却不行!
    【解决方案4】:

    对我来说,%appdata%\NuGet\NuGet.Config 仅包含

    <activePackageSource>
      <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
    </activePackageSource>
    

    做完之后

    nuget sources add -Name "NuGet official package source" -Source "https://api.nuget.org/v3/index.json"
    

    这是添加的

    <packageSources>
      <add key="https://www.nuget.org/api/v2/" value="https://www.nuget.org/api/v2/" />
      <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
    </packageSources>
    

    后来发现包。

    【讨论】:

      【解决方案5】:

      我遇到了类似的问题。它无法找到这些包裹,因为它正在寻找它们离线存储在我的计算机中而不是在线。 Visual Studio NuGet 设置屏幕未显示此内容,但错误消息显示。我对离线包不感兴趣,所以解决它(windows):

      首先进入 Visual Studio 2019,工具>Nuget 包管理器>包管理器设置>包源并添加:https://api.nuget.org/v3/index.json(如果它不存在),将其命名为 nuget.org。执行此操作后我的错误仍然存​​在,因此接下来的步骤。

      按开始并输入 %appdata% 以将您带到 C:/Users/[用户名]/AppData/Roaming。导航到 NuGet 目录并打开 NuGet.Config。

      所有包源都显示在此处,即使它们没有出现在 Visual Studio 包管理器设置中。

      修改 PackageSources 部分以删除您不需要的源。就我而言,我不需要离线源,所以我只剩下:

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

      这为我解决了这个问题。

      【讨论】:

        【解决方案6】:

        开始在我们的 Azure DevOps 构建管道中偶尔出现这些错误,但从未在本地出现。

        示例错误:

        错误 NU1101:无法找到包 Microsoft.EntityFrameworkCore.Sqlite。不存在具有此 ID 的软件包 来源:Microsoft Visual Studio 离线包

        如果我们重新启动构建,它可以工作,但它又发生了。

        然后我注意到它总是发生在我们的测试项目中,而不是我们的主 Web 项目中。

        我们使用.NET/NuGet 管道缓存来加速我们的构建。我们使用如下所示的默认版本:

        variables:
          NUGET_PACKAGES: $(Pipeline.Workspace)/.nuget/packages
        
        steps:
        - task: Cache@2
          inputs:
            key: 'nuget | "$(Agent.OS)" | **/packages.lock.json,!**/bin/**'
            restoreKeys: |
               nuget | "$(Agent.OS)"
            path: $(NUGET_PACKAGES)
          displayName: Cache NuGet packages
        

        https://docs.microsoft.com/en-us/azure/devops/pipelines/release/caching?view=azure-devops#netnuget

        唯一的变化是微软使用$(UserProfile),但我们使用$(Pipeline.Workspace)作为变量。使用 $(UserProfile) 时出错,您可以在此处阅读更多信息:

        https://github.com/MicrosoftDocs/azure-devops-docs/issues/10110

        它读取packages.lock.json,需要通过编辑您的项目文件 (.csproj) 来启用。

        <PropertyGroup>
            <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
        </PropertyGroup>
        

        https://devblogs.microsoft.com/nuget/enable-repeatable-package-restores-using-a-lock-file/

        原来我们只为我们的网络项目生成了一个packages.lock.json,而不是我们的测试项目。将RestorePackagesWithLockFile 添加到我们的测试项目后,它又开始工作了。

        【讨论】:

          【解决方案7】:

          我通过添加依赖文件解决了这个问题

          <files>
              <file src="bin\$configuration$\netstandard2.0\a.dll" target="lib\netstandard2.0" />
              <file src="bin\$configuration$\net45\a.dll" target="lib\net45" />
              <file src="bin\$configuration$\netstandard2.0\b.dll" target="lib\netstandard2.0" />
              <file src="bin\$configuration$\net45\b.dll" target="lib\net45" />
            </files>
          

          另外,不要忘记清理本地缓存nuget locals all –clean 或更改版本。

          【讨论】:

            【解决方案8】:

            我能够通过在 nuget 包源(工具 -> Nuget 包管理器 -> 包管理器设置 -> 包源)中添加更多包源链接来解决此问题。

            在尝试上述解决方案之前,我尝试过,清除 nuget 缓存,更新 targetFramework,重新安装包,更新包管理器,但都没有工作。

            【讨论】:

              【解决方案9】:

              我有一个类似的问题,我定义了多个包源,并且 api.nuget.org 由于另一个源中的故障而没有被命中。我通过使用--ignore-failed-sources 选项来缓解这种情况,例如:

              dotnet tool install -g dotnet-ef --ignore-failed-sources
              

              输出:

              C:\Program Files\dotnet\sdk\5.0.302\NuGet.targets(131,5): warning NU1801: Unable to load the service index for source [[REDACTED]]. [c:\temp\3iqdpc5w.qpj\restore.csproj]
              You can invoke the tool using the following command: dotnet-ef
              Tool 'dotnet-ef' (version '5.0.8') was successfully installed.
              

              已通过以下方式验证安装:

              dotnet tool list -g
              

              输出:

              Package Id      Version      Commands 
              --------------------------------------
              dotnet-ef       5.0.8        dotnet-ef
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-08-31
                • 1970-01-01
                • 1970-01-01
                • 2019-09-10
                • 2019-06-24
                • 1970-01-01
                • 1970-01-01
                • 2019-06-22
                相关资源
                最近更新 更多