Mark Worrall
Mark Worrall

Reputation: 351

Visual Studio 2022 not including dependent dlls in NuGet package

This should be easy. I have a .Net Core class library solution, 'XxxApi', that is dependent on 2 other dlls in the solution: 'YyyRepository' and 'ZzzShared'. I build and package up 'XxxApi' as a NuGet package, but it never includes the 2 dependent .dll binaries it needs?

If I unzip it I can see them declared as dependencies, but only XxxApi.dll is in the lib folder?

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>XxxApi</id>
    <version>1.0.0</version>
    <title>API to XXX database</title>
    <authors></authors>
    <description>A programmable Api that can be used to query and update tables in the XXX database.</description>
    <copyright>xxx (c) 2023</copyright>
    <tags>API XXX Database</tags>
    <dependencies>
      <group targetFramework="net6.0">
        <dependency id="YyyRepository" version="1.0.0" exclude="Build,Analyzers" />
        <dependency id="ZzzShared" version="1.0.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.EntityFrameworkCore" version="7.0.5" exclude="Build,Analyzers" />
        <dependency id="Microsoft.EntityFrameworkCore.SqlServer" version="7.0.5" exclude="Build,Analyzers" />
        <dependency id="Microsoft.Extensions.Configuration.FileExtensions" version="7.0.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.Extensions.Configuration.Json" version="7.0.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="C:\DEV\Dis\XxxApi\XxxbApi\bin\Release\net6.0\win-x64\XxxApi.dll" target="lib\net6.0\XxxApi.dll" />
  </files>
</package>

I copy the package to 'Local Package source'.

This package is used in multiple client apps and when I include it from 'Local Package source', the build fails saying it cannot find the 2 dependent dlls.

If I build the 2 dependent dlls as NuGet packages and just copy them to 'Local Package source' (and not add them to any client app) the client app builds and runs ok (?), but I shouldn't have to do that.

I have researched it on and off for a couple of days and read some convoluted solutions, but most seem related to earlier versions of Visual Studio, .Net or NuGet. Some said to edit the .nuspec file, but any edits just get lost on the next publish. I read about resolving dependencies, but they talk about dependent NuGet packages. I even saw the recommendation to always have 1 .dll per NuGet package, which is ludicrous when these dlls always go together.

Surely, by default, if you build a NuGet package it should include the dependent binaries that are part of the solution (i.e. not in NuGet packages)? Has anyone else run into this issue and know how to solve it? (I assume by getting the other 2 ddls declared under <files> somehow)

Upvotes: 2

Views: 2120

Answers (2)

Bowman Zhu
Bowman Zhu

Reputation: 7241

I think you need something like this:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>test</id>
    <version>1.0.0</version>
    <authors>BowmanZhu</authors>
    <owners>BowmanZhu</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>test</description>
    <releaseNotes>test</releaseNotes>
  </metadata>
  <files>
    <file src="bin\Debug\net6.0\XxxApi.dll" target="lib\net6.0\XxxApi.dll" />
    <file src="bin\Debug\net6.0\YyyRepository.dll" target="lib\net6.0\YyyRepository.dll" />
    <file src="bin\Debug\net6.0\ZzzShared.dll" target="lib\net6.0\ZzzShared.dll" />
  </files>
</package>

Result:

enter image description here

Upvotes: 0

Narthring
Narthring

Reputation: 1419

When you package up XxxApi I wouldn't expect the binary for the dependencies to be downloaded and included in the package. I'd expect the dependencies to be resolved when the multiple client apps perform a NuGet restore against XxxApi. The multiple client apps should resolve dependencies accordingly.

Upvotes: 0

Related Questions