如何为 System.Data.SQLite 创建我自己的自定义 nuget 包以正确包含互操作 dll?

问题描述

我想为 System.Data.sqlite 创建我自己的自定义包。我有我需要的所有 dll,但我不确定如何构建它并为它创建 nuspec。 dll 的当前文件夹结构是这样的,我会在哪里放置不同的互操作 dll 以便将它们正确复制到输出中,我需要将什么添加到 nuspec?

lib/net452
  -> System.Data.sqlite.dll,System.Data.sqlite.Linq.dll,System.Data.sqlite.EF6.dll
Custom.sqlite.nuspec

仍然有像这样的认 nuspec 提款机

 <?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/01/nuspec.xsd">
  <Metadata minClientVersion="2.5">
    <id>Custom.sqlite.Name</id>
    <version>1.0.0.0</version>
    <authors>name</authors>
    <owners>owner</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Desc</description>
    <copyright>copyright 2021</copyright>
  </Metadata>
</package>

解决方法

SQLite.Interop.dll 不充当 lib 程序集 dll。那不是它的作用。它应该是一个内容文件而不是一个程序集 dll。因此不应将其打包为 lib

要创建此类自定义 nuget 包,您应该首先将 System.Data.SQLite.dllSystem.Data.SQLite.Linq.dllSystem.Data.SQLite.EF6.dll 打包为 lib。见this document

然后将 SQLite.Interop.dll 打包为 content

另外,要在安装nuget包时将内容文件复制到主项目的输出文件夹中,必须使用.props或targets文件来实现.

1) 在您的类库项目中创建一个名为 .props 的文件。它应该与您的 nuget 包同名。在你这边,它应该被命名为 Custom.SQLite.Name.props。否则,它将无法工作。

然后将这些添加到文件中:

<Project>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)..\content\x86\SQLite.Interop.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
        <None Include="$(MSBuildThisFileDirectory)..\content\x64\SQLite.Interop.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

</ItemGroup>

</Project>

2) 使用这个 nuspec 文件:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/01/nuspec.xsd">
  <metadata minClientVersion="2.5">
    <id>Custom.SQLite.Name</id>
    <version>1.0.0.0</version>
    <authors>name</authors>
    <owners>owner</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Desc</description>
    <copyright>Copyright 2021</copyright>
  </metadata>
<files>
<file src="System.Data.SQLite.dll" target="lib\net452" />
<file src="System.Data.SQLite.EF6.dll" target="lib\net452" />
<file src="System.Data.SQLite.Linq.dll" target="lib\net452" />
<file src="xxx\x86\SQLite.Interop.dll" target="content\x86" />
<file src="xxx\x64\SQLite.Interop.dll" target="content\x64" />
<file src="Custom.SQLite.Name.props" target="build" />

</files>
</package>

3) 重建你的 lib 项目,然后使用 nuget pack 打包新版本。

使用这个新版本之前,请卸载旧版本并删除C:\Users\xxx\.nuget\packages\Custom.SQLite.Name<solution_folder>\packages\Custom.SQLite.Name.1.0.0下的所有缓存文件。然后,重新安装新版本。