如何以现代通用方式打包具有以下属性的 .NET 库?
这是一系列问题和答案,记录了我对现代 NuGet 包创作主题的发现,特别关注 NuGet 3 引入的更改。您可能还对一些相关问题感兴趣:
此答案基于用于打包 .NET Framework 库的原则、用于打包通用 Windows 平台库的原则以及用于打包可移植库的原则。首先阅读链接的答案以更好地理解以下内容。
为了服务所描述的平台集,您需要将解决方案相应地构造为多个类库项目:
您将希望实现以下 NuGet 包结构:
\---lib
+---dotnet
| MyPortableLibrary.dll
| MyPortableLibrary.pdb
| MyPortableLibrary.XML
|
+---net46
| MyDotNetLibrary.dll
| MyDotNetLibrary.pdb
| MyDotNetLibrary.XML
| MyPortableLibrary.dll
| MyPortableLibrary.pdb
| MyPortableLibrary.XML
|
\---uap10.0
| MyPortableLibrary.dll
| MyPortableLibrary.pdb
| MyPortableLibrary.XML
| MyUwpLibrary.dll
| MyUwpLibrary.pdb
| MyUwpLibrary.pri
| MyUwpLibrary.XML
|
\---MyUwpLibrary
HashControl.xaml
MyUwpLibrary.xr.xml
如果您熟悉上面链接的其他答案,那么这对您来说应该相对熟悉。唯一的特殊部分是可移植库存在三个副本,因为它的内容将提供给所有目标平台。
可以通过使用基于以下模板的 nuspec 文件来实现所需的包结构:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata minClientVersion="3.2">
<id>Example.MyMultiSurfaceLibrary</id>
<version>1.0.0</version>
<authors>Firstname Lastname</authors>
<description>Example of a multi-platform library that exposes different API surfaces to .NET 4.6 and UWP and also includes a portable component.</description>
<dependencies>
<!-- UWP has more dependencies than other platforms (Newtonsoft.Json). -->
<group targetFramework="uap10.0">
<dependency id="Newtonsoft.Json" version="8.0.1" />
<dependency id="System.Linq" version="4.0.0" />
<dependency id="System.Numerics.Vectors" version="4.1.0" />
<dependency id="System.Resources.ResourceManager" version="4.0.0" />
<dependency id="System.Runtime" version="4.0.20" />
</group>
<!-- All other platforms - just the dependencies of the portable library here. -->
<group>
<dependency id="System.Linq" version="4.0.0" />
<dependency id="System.Numerics.Vectors" version="4.1.0" />
<dependency id="System.Resources.ResourceManager" version="4.0.0" />
<dependency id="System.Runtime" version="4.0.20" />
</group>
</dependencies>
</metadata>
<files>
<file src="..\bin\Release\MyPortableLibrary.*" target="lib\net46" />
<file src="..\bin\Release\MyPortableLibrary.*" target="lib\uap10.0" />
<file src="..\bin\Release\MyPortableLibrary.*" target="lib\dotnet" />
<file src="..\..\MyDotNetLibrary\bin\Release\MyDotNetLibrary.*" target="lib\net46" />
<!-- Double wildcard also ensures that the subdirectory is packaged. -->
<file src="..\..\MyUwpLibrary\bin\Release\MyUwpLibrary**" target="lib\uap10.0" />
</files>
</package>
请注意,定义了两组独立的依赖项:一组通用组,一组特定于通用 Windows 平台,因为 UWP 库对 Newtonsoft.Json 包有额外的依赖项。您可以将相同的模式扩展到具有特定于平台的依赖性的任意数量的平台。
就是这样 - 这个 NuGet 包现在可以安装到 .NET Framework 4.6 项目、通用 Windows 平台项目和面向兼容 API 表面的可移植库项目中。可移植库的功能将在所有平台上导出,特定于平台的库也在适当的平台上使用。
请记住在创建 NuGet 包之前使用发布配置构建解决方案。
示例库和相关打包文件可在 GitHub 上获取。这个答案对应的解决方案是MultiSurfaceLibrary。
Sander,我使用相同的 nuspec 文件(WPF .NET Framework、WPF Core 和 UWP)方案,我在 UWP 部分的依赖项中有“Microsoft.UI.Xaml”,但是当我将 nuget 用于 WPF .NET Framework 项目时我在项目的输出文件夹中得到了这个文件“Microsoft.UI.Xaml”,并且在 WACK 中遇到了危险的 wsrning,并且在 Microsoft Store 中遇到了错误 pudlising。 为什么我从 WPF 项目中的 UWP 部分“Microsoft.UI.Xaml”获取依赖项?