MaYaN
MaYaN

Reputation: 6986

How can I include a NuGet package with native dlls in my new CSPROJ Class Library?

I have a class library which I just migrated to the new csproj format that has a dependency on SQLite.Core.

SQLite has native dependencies x64/SQLite.Interop.dll and x86/SQLite.Interoop.dll which used to get copied to the output folder for any project adding my package. This was taken care of by NuGet by automatically including the following import in the old csproj file:

<Import Project="..\packages\System.Data.SQLite.Core.1.0.106\build\net451\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.106\build\net451\System.Data.SQLite.Core.targets')" />

After migrating to the new csproj I can build my library as a NuGet package which correctly includes SQLite.Core as a dependency however whoever consumes my NuGet library does not get the native dependencies coppied to the output folder.

On the other hand, if the consumer first adds the SQLite.Core then add my package everything works correctly.

Any idea how I can restore the behaviour before the migration? Below is the migrated csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <PackageId>My.Library</PackageId>
    <Description>Some Library.</Description>        
  </PropertyGroup>

  <PropertyGroup>
    <TargetFramework>net452</TargetFramework>    
    <AssemblyName>My Library</AssemblyName>    
  </PropertyGroup>

  <ItemGroup>    
    <PackageReference Include="System.Data.SQLite.Core" Version="1.0.106" />
  </ItemGroup>

    <ItemGroup>
      <Reference Include="Microsoft.CSharp" />
    </ItemGroup>    

</Project>

Upvotes: 2

Views: 2591

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100543

Since System.Data.SQLite.Core uses build-time logic to include the package to the content, the default dependency settings for PackageReference aren't a good fit if you build a library referencing that package. By default, PackageReference will not forward contentFiles, build assets and analyzers for transitive references.

To change that behavior, edit your PackageReference to:

<PackageReference Include="System.Data.SQLite.Core" Version="1.0.106" PrivateAssets="none"/>

This will ensure that the package will behave exactly the same when reference directly or transitively via your library. In your specific case, PrivateAssets could also be contentFiles;analyzers since only build would need to be forwarded but since the package only contains build assets, it doesn't matter.

See Controlling dependency assets for more details.

Upvotes: 4

Related Questions