【问题标题】:How to package a .NET library targeting .NET Framework and Universal Windows Platform and include platform-specific functionality?如何打包面向 .NET Framework 和通用 Windows 平台的 .NET 库并包含特定于平台的功能?
【发布时间】:2016-04-09 07:04:05
【问题描述】:

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

  • 为 .NET Framework 4.6 和通用 Windows 平台提供一些共享功能。
  • 为每个平台提供一些特定于平台的功能(例如,专门的类或 API,包括用于 UWP 的 XAML 用户控件),并可能依赖于特定于平台的外部库。
  • 与架构无关 (AnyCPU)。
  • 其可移植子集可供其他以兼容 API 表面为目标的可移植库使用。

这是一系列问题和答案,记录了我对现代 NuGet 包创作主题的发现,特别关注 NuGet 3 引入的更改。您可能还对一些相关问题感兴趣:

【问题讨论】:

    标签: .net nuget uwp nuget-package .net-core


    【解决方案1】:

    此答案基于principles used to package .NET Framework librariesprinciples used to package Universal Windows Platform librariesprinciples used to package portable libraries。首先阅读链接的答案以更好地理解以下内容。

    要服务于所描述的平台集,您需要将解决方案相应地构建为多个类库项目:

    • .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 包之前使用发布配置构建您的解决方案。

    一个示例库和相关的打包文件是available on GitHub。这个答案对应的解决方案是MultiSurfaceLibrary。

    【讨论】:

      猜你喜欢
      • 2016-04-09
      • 2016-04-09
      • 2016-04-09
      • 2010-11-09
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      相关资源
      最近更新 更多