3

I need to create a Nuget package out of my library .net framework project.

The problem is that my project depends on many other dlls which I do not want my Nuget users to see. So I want people to install my package and see only my project dll in the references list, while all its dependencies will be added to bin folder only.

Actually, this is what happens when I reference this dll in any project- I can see it in my references list, and all its dependencies are simply copied to bin folder right after building. Can this behaviour be achieved with this project as Nuget ?

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

3 Answers3

1

Create a Nuget package without its dependencies to be added as references

Since you do not want your Nuget users to see the other dlls, so you can not set those dlls file as dependencies, which will be added to the references list by default.

To resolve this issue, we need add those dlls file in the other folder in the .nuspec file and add a function to copy this dlls file to the bin folder when we add this nuget package to the project. You can follow below steps:

  1. Add a xx.targets file in your project folder(The one you used to create nuget package ), make sure the name of the target file is the same name as the package id(TestDemo is my package ID, so the name of .targets is TestDemo.targets).

  2. Add below code in the targets file:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <ItemGroup>
      <None Include="$(MSBuildThisFileDirectory)*.dll">
         <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
     </ItemGroup>
    </Project>
    

Note: The path of "$(MSBuildThisFileDirectory)" should be relative path, if you are not familiar with it, you can use the absolute path.

  1. In the nuspec file, add required file to the Build directory along with the targets file.

      <files>
        <file src="<ThoseDllsPath>\*.dll" target="Build\" />
        <file src="TestDemo.targets" target="Build\" />
        <file src="bin\Debug\TestDemo.dll" target="lib\462" />
      </files>
    
  2. Pack this package, then add it on other project to test, it work fine.

enter image description here

enter image description here

Hope this helps.

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

I tried the solution of Leo Liu-MSFT. It worked in a .NET Framework Project, but not in a .NET Core 3.0

Oktay Myumyunov
  • 120
  • 1
  • 16
0

If you're using packages.config to manage your library's NuGet dependencies and building the nupkg with nuget.exe pack, then I believe you'll need to generate your own nuspec file and simply not add your build-time dependencies as a dependency to your package.

If you're using PackageReference, then you can use PrivateAssets

<ItemGroup>
    <!-- ... -->
    <PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0">
        <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <!-- ... -->
</ItemGroup>
zivkan
  • 12,793
  • 2
  • 34
  • 51