构建主项目时,RuntimeIdentifier 在依赖项目中不可用

RuntimeIdentifier not available in dependent project when building main project

我在依赖项目(称为 DAL)中有一个简单的预构建目标:

<Target Name="PreBuild" BeforeTargets="Build">
    <Message Text="RuntimeIdentifier value is: $(RuntimeIdentifier)" Importance="high" />
</Target>

当我构建 DAL 时,一切都按预期工作:

命令:dotnet build DAL\DAL.csproj --runtime linux-x64

输出:RuntimeIdentifier value is: linux-x64

但是当我构建引用 DAL 的项目时,RuntimeIdentifier 的值消失了:

命令:dotnet build Transformer\Transformer.csproj --runtime linux-x64

输出:RuntimeIdentifier value is:

我在构建命令中添加了 --verbosity d 开关,我注意到了这一点:

Removing Properties for project "..\DAL\DAL.csproj":
  TargetFramework
  RuntimeIdentifier

为什么 msbuild 这样做,以及如何在 building/publishing 主项目时将 RuntimeIdentifier 信息传递给 DAL 项目?

.NET Core SDK 版本:3.1.402,所有项目目标 .NET Core 3.1

谢谢!

更新: 我将以下元素添加到我的 csproj 文件中:

<RuntimeIdentifiers>win-x64;linux-x64</RuntimeIdentifiers>

现在输出是:

 Determining projects to restore...
 All projects are up-to-date for restore.
 DAL -> C:\Source\...\DAL\bin\Debug\netcoreapp3.1\linux-x64\DAL.dll
 RuntimeIdentifier value is: linux-x64
 DAL -> C:\Source\...\DAL\bin\Debug\netcoreapp3.1\DAL.dll
 RuntimeIdentifier value is:
 Core -> C:\Source\...\Core\bin\Debug\netcoreapp3.1\Core.dll
 Transformer -> C:\Source\...\Transformer\bin\Debug\netcoreapp3.1\linux-x64\Transformer.dll

似乎解决方法是设置 RuntimeIdentifiers 元素,这是设计使然吗?

这是设计使然。运行时标识符更受将要发布的入口点应用程序项目的关注,而不是库项目。

之前(1.0/预览时间范围)转发了 RID(运行时标识符)并导致大量重建和 NuGet 与 1.0/1.1 运行时 NuGet 包的构建方式不一致。

目前分发 RID-specific 程序集的最佳方法是构建一个 NuGet 包,并将程序集放置在右侧 architecture specific folder

但是,也可以设计运行时代码来检查 RuntimeInformation.IsOSPlatform(OSPlatform.***) 以选择要使用的正确 API 或要加载的正确程序集。