【问题标题】:Azure custom Nuget package external librariesAzure 自定义 Nuget 包外部库
【发布时间】:2021-08-31 12:30:41
【问题描述】:
  • 我创建了一个项目,该项目自动打包到 NuGet 包中并通过管道添加到 Azure Artifacts
  • 我的 NuGet 包使用来自 nuget.org 的名为“TextFieldParserStandard”的库
  • 我通过在 Visual Studio 中添加 Artifacts Feed 包源来安装我的 NuGet
  • 我导入类并使用函数,但收到错误:
System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'TextFieldParserStandard, Version=1.0.0.0,
 Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. 

如果我在使用 NuGet 的项目中手动搜索并添加此库,我的 NuGet 可以正常工作。但它们应该自动出现。当我安装其他软件包时,我不需要手动安装它们的库。 所以问题是我的 NuGet 安装没有立即自动安装“TextFieldParserStandard”。

在 Artifacts Feed 中,我只有我的包“MyNuGet 版本 1.0.0.11538”。我不知道我的提要中是否应该有“TextFieldParserStandard”。 在 Visual Studio 中,管理 NuGetPackages,我的 NuGet 没有任何依赖项。

我的 NuGet SDK:.NET Standard 2.0(注意:与导入的项目兼容,因为如果我手动添加 TextFieldParserStandard,它就可以工作)。

如果需要,我可以提供 yaml 管道。这以某种方式解决了问题。 Build NuGet Package automatically including referenced dependencies

但是在yaml中添加IncludeReferencedProjects并没有解决。

【问题讨论】:

    标签: c# .net azure-devops azure-pipelines nuget-package


    【解决方案1】:

    Azure 自定义 Nuget 包外部库

    那是因为您没有将参考包 TextFieldParserStandard 添加到您的自定义包 MyNuGet 作为依赖项。

    在这种情况下,当您安装包MyNuGet时,它不会自动将参考包TextFieldParserStandard安装到您的项目中。

    要解决此问题,您可以在项目MyNuGet 文件MyNuGet.csproj 中为包TextFieldParserStandard 添加属性<PrivateAssets>all</PrivateAssets>

      <ItemGroup>
        <PackageReference Include="TextFieldParserStandard" Version="1.0.0">
          <PrivateAssets>all</PrivateAssets>
        </PackageReference>
      </ItemGroup>
    

    将此更改提交到 repo,然后为您的自定义包生成新包,它将具有 TextFieldParserStandard 包作为依赖项:

    更新:

    因为TextFieldParser不能在单元测试中被引用。

    您可以尝试使用Dependencies 元素创建您的.nupsec 文件,例如:

    <dependencies>             
      <dependency id="another-package" version="3.0.0" />             
      <dependency id="yet-another-package" version="1.0.0" />        
    </dependencies>
    

    您可以参考this document了解更多详情。

    【讨论】:

    • 该操作后,我在 MyNuget 中的单元测试不再运行,因为错误 Could not load file or assembly。 TextFieldParser 不能在单元测试中被引用。在构建解决方案中,管道也失败了。似乎 PrivateAssets 正在做相反的事情,并告诉该软件包仅用于开发。
    • @MihaiSocaciu,知道了。由于 TextFieldParser 不能在单元测试中引用。您可以尝试使用 Dependencies 元素创建 .nupsec 文件,例如:&lt;dependencies&gt; &lt;dependency id="another-package" version="3.0.0" /&gt; &lt;dependency id="yet-another-package" version="1.0.0" /&gt; docs.microsoft.com/en-us/nuget/reference/…
    • 现在出现依赖关系,但 MyNuGet 1.0.0 does not support any target frameworks MyNuGet 1.0.0 is not compatible with uap10.0.16299 。我在 .nuspec 中试过这个,但没有帮助:``` ``` 我需要在 UWP 应用和 .NET Core 3.1 应用中使用该包。我检查了兼容性图表,甚至在它工作之前。似乎 .nuspec 缺少一些东西。
    • .nuspec 文件解决了依赖问题。如果您编辑您的答案,我可以将其标记为解决方案,我将创建另一个关于目标框架兼容性的问题。似乎无法使用 MyNuget,即使在 .Net Standard 2.0 项目中也是如此。与库相同的 SDK。
    • @MihaiSocaciu,我已经更新了答案。欢迎发布另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 2017-10-14
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多