8

I'm trying to create a NuGet package from a VS 2017 project that contains a reference to a 3rd party DLL (Kendo.mvc.dll). No matter what I try, I can't get nuget pack to automatically include that DLL in my NuGet package's lib folder.

I originally created the .nuspec file from the command line with the command nuget spec [project's name and path].csproj. I then tweaked the settings of that file which resulted in this .nuspec file:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>our names</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>the description</description>
    <releaseNotes>First release</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>entity-framework-6 mvc5</tags>
  </metadata>
</package>

I then used nuget pack to create the package, which resulted in this:

Package contents showing no 3rd party DLL was included

From what I've read in the documentation (MS Docs - Creating NuGet packages) I was expecting nuget pack to automatically include any non-nuget-sourced DLLs referenced in the project in the package, but that's not happening?

I've tried the following to see if it makes any difference, all to no avail:

  • Adding a lib folder to the root of my project and putting the DLL in there (and experimented a bit with changing the Build Action and Copy to Output Directory settings). That created a lib folder under the Content folder in the NuGet package, rather than adding the DLL to the same lib folder that contains the assembly's DLL. I did that because the MS docs talk about a convention based file structure.
  • Referencing the DLL from a folder in C:\Program Files and setting the Copy Local property for the DLL to true in the Properties

This question talks about adding a separate element for the 3rd party DLL which I'm guessing refers to adding the files explicitly in the .nuspec file before generating the package, e.g.

  <files>
    <file src="bin\Debug\Kendo.Mvc.???" target="lib\net461" />
  </files>

That does work, but shouldn't this be added automatically if it's needed when generating a .nuspec file from a csproj file?

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
tomRedox
  • 28,092
  • 24
  • 117
  • 154

2 Answers2

10

If you want to do it through Visual Studio or dotnet, then you can edit your csproj file, add an ItemGroup to include the dlls as below: This will pack the other dlls along with your current project dll in the nuget package.

<ItemGroup>
    <Content Include="<path to other dll>">
        <Pack>true</Pack>
        <PackagePath>lib\$(TargetFramework)</PackagePath>
    </Content>
</ItemGroup>
Akhil Dabral
  • 760
  • 1
  • 10
  • 11
  • This should be the accepted answer. Worked perfectly for me. – On The Net Again May 09 '21 at 17:34
  • 1
    $(TargetFramework) does not working for VS2022. It just place my external dll outside the lib folder. I had to explicitly put lib\net6.0. And if my project multitarget, I need to have another similar block of ... with the other target framework sepcified – Tri Jun 02 '23 at 19:24
3

but shouldn't this be added automatically if it's needed when generating a .nuspec file from a csproj file?

Yes, the non-nuget-sourced DLLs were not added automatically by default when generating a .nuspec file from a .csproj file.

When we generate the .nuspec file from a .csproj file, the .nuspec only includes the basic info, for example, Title, Description, and so on. You can get this info from Assembly Information, Properties->Application-> Assembly Information:

enter image description here

But it does not include the 3rd party DLL by default.

So, in order to include third party DLLs, we need manually add the files explicitly in the .nuspec file, jusy like what you did.

Check Create nuget package from dlls for more details.

BTW, if the 3rd party DLL is nuget-sourced DLLs referenced in the project, you can use the parameter -IncludeReferencedProjects to add it automatically when generating a .nuspec file from a csproj file.

Document: pack command (NuGet CLI)

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135