Search code examples
xmlvisual-studionugetnuspec

NuGet package differences depending on build method


I have a .NET Core project that I want to pack as a NuGet package to be used in other Visual Studio projects/solutions. In .NET Core projects, there is this nice feature where you can select in the project properties to generate a NuGet package on build, which I tried. The result is a package, let's call it My.Company.Package, which has a generated nuspec file that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>My.Company.Package</id>
    <version>10.1.2</version>
    <authors>My.Company.Package</authors>
    <owners>My.Company.Package</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package Description</description>
    <dependencies>
      <group targetFramework=".NETCoreApp3.1">
        <dependency id="Microsoft.Win32.Registry" version="4.7.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="C:\dev\MyProduct\bin\Debug\netcoreapp3.1\My.Company.dll" target="lib\netcoreapp3.1\My.Company.dll" />
  </files>
</package>

When installing the built package in another solution, the NuGet package manager says the following:

Installing:

Microsoft.NETCore.Platforms.3.1.0
Microsoft.Win32.Registry.4.7.0
My.Company.Package.10.1.2
System.Security.AccessControl.4.7.0
System.Security.Principal.Windows.4.7.0

After installing it, the package seems to work fine, I can access the namespaces and classes that are supposed to be included in the package.

For reasons I won't go into here, I have to pack the package myself, without this option. I have set up a post-build event in Visual Studio that runs nuget pack mycompany.nuspec, and the mycompany.nuspec looks like this (I tried to imitate the generated one):

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>My.Company.Other.Package</id>
    <version>10.1.214</version>
    <authors>My Company</authors>
    <description>
    The real description.
    </description>
    <copyright>© My company</copyright>
    <tags>My company</tags>
    <dependencies>
      <group targetFramework=".NETCoreApp3.1">
        <dependency id="Microsoft.Win32.Registry" version="4.7.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="C:\dev\MyProduct\bin\Debug\netcoreapp3.1\My.Company.dll" target="lib\netcoreapp3.1\My.Company.dll" />
  </files>
</package>

The package is built successfully. However, when I select to install it in an external project, the NuGet package manager notifies me that it will only install this specific package:

Installing:

My.Company.Package.10.1.2

After installing it, I can not access the expected classes or namespaces, it doesn't seem to have included everything that is needed.

How can the first option install so many packages that are never listed as dependencies in the nuspec file, and how can I imitate it?


Solution

  • I have no idea what exactly did it, but after cleaning and rebuilding the solution where I pack from, and restarting Visual Studio, it worked.