如何打包一个针对.NET Framework和通用Windows平台的.NET库并包含特定于平台的功能?

4

如何以现代通用的方式打包一个具有以下特性的.NET库?

  • 为.NET Framework 4.6和Universal Windows Platform提供一些共享功能。
  • 为每个平台提供一些特定功能(例如专用类或API,包括UWP的XAML用户控件),并可能依赖于外部库的特定平台。
  • 架构无关(AnyCPU)。
  • 其可移植子集可以被其他针对兼容API表面的可移植库使用。
这是一系列关于现代NuGet包制作的问题和答案,特别关注NuGet 3引入的变化。您可能还对以下相关问题感兴趣: - 如何打包.NET Framework库? - 如何打包针对通用Windows平台的.NET库? - 如何打包针对.NET Core的可移植.NET库? - 如何打包针对通用Windows平台的多架构.NET库? - 如何打包针对通用Windows平台的依赖于Visual Studio扩展SDK的.NET库?
1个回答

2
此答案基于打包.NET Framework库所使用的原则、打包通用Windows平台库所使用的原则以及打包可移植库所使用的原则。请先阅读相关链接以更好地理解以下内容。
为了服务于上述平台集,您需要相应地将解决方案结构化为多个类库项目:
  • 一个针对.NET Framework 4.6的特定平台类库。
  • 一个针对通用Windows平台的特定平台类库。
  • 一个针对公共API表面的可移植库。
您将想要实现以下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 包之前,请记得使用 Release 配置构建解决方案。 GitHub 上提供了示例库和相关打包文件。与本答案对应的解决方案是 MultiSurfaceLibrary。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接