首页 > 解决方案 > 如何在 net472 上正确打包和使用具有多个运行时的 .NET Standard 库?

问题描述

我正在尝试使用这个项目为 Python.NET 制作一个多平台包,以 .NET Standard 2.0 为目标。

我设法创建了一个具有以下结构的 NuGet 包:

ref
|- netstandard2.0
 |- Python.Runtime.dll
runtimes
|- win
 |- lib
  |- netstandard2.0
   |- Python.Runtime.dll
|- linux-x64
 |- ...

这个包与基于新的 .csproj 格式 + 的测试应用程序完美配合PackageReferece,但前提是我的目标是netcoreappX.X. 在这种情况下,它将整个runtimes文件夹复制到输出dotnet publish并运行没有问题。

如果我更改TargetFrameworknet472,则Windowsdotnet builddotnet publishWindows 都无法从包中复制任何版本的 Python.Runtime.dll,从而导致FileNotFoundException: Could not load file or assembly 'Python.Runtime, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

尝试从 Visual Studio 运行时会引发相同的异常。

该异常实际上是有道理的,因为我可以看到程序集没有复制到输出目录。

现在的问题是以下哪个是问题,以及如何解决它:

  1. 我的 .nuget 包缺少某些东西,因此没有为目标拾取任何东西net472(尽管我希望它只是使用netstandard2.0东西)
  2. 我的测试应用程序 .csproj 缺少一些东西(也许我需要在 .csproj 中指定所有支持的运行时?)
  3. 工具根本不支持这样的设置

如果上述问题有解决方案,它是否也适用于旧式 .NET Framework 项目packages.config

标签: .net.net-corenuget.net-standard-2.0.net-4.7.2

解决方案


正如 Oren Novotny 在https://github.com/dotnet/cli/issues/10806中所建议的,有两件重要的事情。

使用dotnet rundotnet publishnet4xx目标一起使用时,在这种情况下--runtime必须指定。

对于dotnet publish一个通用的似乎很好:dotnet run -r win.

因为dotnet run我必须指定一个具体的:dotnet run -r win7-x64.

为避免这种情况,除了当前内容(尚未尝试)之外,我还必须将 lib/net4XX/ 添加到包中。


推荐阅读