DEV Community

Julian-Chu
Julian-Chu

Posted on

[C#] including 3rd DLL file to nuget package

In one IOT solution with c#, I have to use nuget package instead of .Net Standard library project reference. In library project, my c# code provide methods to call 3rd dll file to manipulate device, other projects don't know the dll, just call methods of my library project.
It works well using project reference, but after generating nuget package from library package and replacing all package reference by nuget package, the program throws exception that dll file doesn't found. After checking the nuget package, I found the 3rd dll not included in nuget package.

Before we could do this by writing a .nuspec, but it's not necessary for .Net standard library, just need to add some setting to .csproj.

  • setup Build Action to Content ( I have tried embedded resource, but not work)
  • add <Pack/><PackagePath/>

ps: after building nuget package, dll file complied from c# code locates under lib(targetframework) folder, so we have to place the 3rd dll file together.

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

  <PropertyGroup>
    ................
  </PropertyGroup>


  <ItemGroup>
    <Content Include="3rdParty.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <Pack>true</Pack>
      <PackagePath>lib\$(TargetFramework)</PackagePath>
    </Content>
  </ItemGroup>

</Project>

It takes me more than one day to solve it. Hope this note can help someone.

Top comments (3)

Collapse
 
tonyjoanes profile image
Tony Joanes

this didnt work for me, the file was include inside the nuget package but when I install it the file isn't included

Collapse
 
julianchu profile image
Julian-Chu

hi Tony, sorry for late reply. I don't know your project detail. I must say it's weird if dll is already inside nuget package, maybe clean nuget cache or publish new version nuget package can help.

Collapse
 
vasar007 profile image
Vasily Vasilyev

I faced the same issue as Tony. The issue was resolved by deleting node <PackagePath />. My final configuration:

 <ItemGroup>
    <Content Include="$(SolutionDir)Libraries\lib.so">
      <Pack>true</Pack>
      <BuildAction>None</BuildAction>
      <PackageCopyToOutput>true</PackageCopyToOutput>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
Enter fullscreen mode Exit fullscreen mode

If node <PackagePath /> exists, file will be added to NuGet package. However, this file cannot be copied to the output directory during build process of some project which includes such NuGet package. Also you can check autogenerated NuGet specification here:

C:\Users\USERNAME.nuget\packages\PACKAGENAME\VERSION\PACKAGENAME.nuspec

If there are no <contentFiles /> node, then your file will not be copied to the output directory. You should remove <PackagePath /> or disable autogeneration of NuGet specification and write it by youselft and specify files to copy.