【问题标题】:Complex NuGet dependency management复杂的 NuGet 依赖管理
【发布时间】:2019-09-09 23:20:25
【问题描述】:

我想提供一个我创建的项目的 NuGet 包,但我在“隐藏”依赖项时遇到了一些问题。

下面的配置可以吗?

  • 不要将项目引用作为 NuGet 依赖项。
  • 允许消费者使用依赖项 A 中的代码。
  • 对消费者完全隐藏依赖项 B。

要删除所有 NuGet 依赖项并隐藏 B 类依赖项,我尝试了以下操作:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<!-- PrivateAssets to remove NuGet depencies -->
<ItemGroup>
    <ProjectReference Include="..\A.csproj" PrivateAssets="All" />
    <ProjectReference Include="..\B.csproj" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
    <BuildOutputInPackage Include="$(OutputPath)A.dll" />
    <!-- B is hidden, so not included -->
</ItemGroup>

问题是,在运行时,内部 NuGet 代码崩溃,因为它找不到依赖项 B 及其依赖项。 (这很明显,因为 DLL 不存在。)

【问题讨论】:

    标签: .net nuget csproj


    【解决方案1】:

    PrivateAssets="All" 只有在你对该包有构建时间依赖时才有意义。

    您想要的是控制 B 的资产流入项目本身。 这意味着您要禁止消费者针对 B 进行编码。

    实现这一点的方法是使用ExcludeAssets="compile" 而不是 PrivateAssets。

    https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets

    所以你会得到以下内容:

    <ItemGroup>
        <ProjectReference Include="..\A.csproj"/>
        <ProjectReference Include="..\B.csproj" ExcludeAssets="compile" />
    </ItemGroup>
    

    【讨论】:

    • 这不允许我编译。它使用 PrivateAssets 编译,但我仍然有 NuGet 依赖项。
    猜你喜欢
    • 2012-12-26
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    相关资源
    最近更新 更多