'Automatically update .nupec file with dependencies

I've created a nuget package that is used by another project.

My package references Microsoft.Identity.Web version 1.23.1. When I pull this package into the project where I want to you use it, I don't get told that my package has this dependency, I understand I can add the following to the .nuspec file to fix this:

    <dependencies>
        <dependency id="Microsoft.Identity.Web" version="1.23.1" />
    </dependencies>

Now I'll get the message about also downloading this package when I install my own package. I would like this to happen automatically, I don't want to have to go into here and update the version numbers and/or add any new dependencies when I update my package.

I'm sure there is a very basic answer to this but I just can't seem to find the answer.

My .csproj for my package:

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Identity.Web" Version="1.23.1" />
  </ItemGroup>

  <ItemGroup>
    <None Update=".nuspec">
      <SubType>Component</SubType>
    </None>
  </ItemGroup>

</Project>


Solution 1:[1]

The right way is to not use a nuspec at all - they're a bit legacy, and since you're using PackageReference dotnet can get all the info it needs from your project file.

See https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets#pack-target for how to use your project file to specify any other values you've currently got in your nuspec

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rbennett485