Search code examples
c#referencevisual-studio-2017dependenciesnuget

NuGet packages project dependencies as nuget dependencies


I have 3 class library projects (all .NET Standard 2.0) that are all in the same solution. I want to package them into a single nuget and use the code in other repos.

However, when I package them into a NuGet package two of them are added as a nuget dependency to the third one, instead of being directly referenced as dlls.

Here is an example of my setup.

The 3 projects - A.csproj, B.csproj, C.csproj (All Class Libraries, All .NET Standard 2.0)

A is set as a startup project and references B and C

B has a reference to C

C has no references to the other two (it only references 2 3rd party nugets)

When I package my solution into a nuget package, the nuspec file has a <dependencies> group that has all nuget references from my project (correct) along with 2 dependencies for projects B and C with versions 1.0.0 (incorrect)

I am not sure what is causing nuget to behave like this (I imagine its by design) but I cannot wrap my head around to fix the issue.

What I want is for projects B and C to be packaged as DLLs to project A and not as seprate packages.


Solution

  • I am not sure what is causing nuget to behave like this (I imagine it's by design) but I cannot wrap my head around to fix the issue.

    Yes, this behavior is by design. When we pack the .net core/.NET Standard, the PrivateAssets metadata tags control whether dependency assets flow to the parent project. If the value is set to All, these dependency assets would not flow to the parent project. In other words, projects B and C will not be added to the package as dependencies.

    Check Controlling dependency assets for more info.

    What I want is for projects B and C to be packaged as DLLs to project A and not as separate packages.

    Just like what I said in the first question, we could use PrivateAssets metadata tags to control whether dependency assets flow to the parent project, but the PrivateAssets metadata tags do not package project B and C as project A DLLs.

    If you want projects B and C to be packaged as DLLs to project A and not as separate packages, we need to use a .nuspec file to include them manually.

    The .nuspec would look like:

    <?xml version="1.0"?>
    <package >
      <metadata>
        <id>TestDemo</id>
        <version>1.0.0</version>
        <authors>Tester</authors>
        <owners>Tester</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>TestDemo</description>
        <releaseNotes>Summary of changes made in this release of the package.    </releaseNotes>
        <copyright>Copyright 2017</copyright>
        <tags>Tag1 Tag2</tags>
      </metadata>
      <files>
        <file src="bin\debug\projectA.dll" target="lib\.netstand2.0" />
        <file src="<Path>\projectB.dll" target="lib\.netstand2.0" />
        <file src="<Path>\projectC.dll" target="lib\.netstand2.0" />
      </files>
    </package>
    

    Check this thread for more details.