Till now, I was always using attributes to control asset dependency for PackageReference
(first example). Recently, after using NuGet package manager (in VS), I ended up with quite different XML though.
Both are controlling same thing, could anyone help me understanding difference and need of having two ways how to control similar thing?
Additions to the csproj format for .NET Core > PackageReference
<PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0" PrivateAssets="..." IncludeAssets="..." ExcludeAssets="..." />
Package references (PackageReference) in project files > Controlling dependency assets:
<PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0">
<IncludeAssets>...</IncludeAssets>
<ExcludeAssets>...</ExcludeAssets>
<PrivateAssets>...</PrivateAssets>
</PackageReference>
These are functionally equivalent. With one exception, using attributes on the <PackageReference>
element is a shortcut for having a nested elements by the same name. The exception is the Include
attribute, which cannot be a nested element.
So, Visual Studio, MSBuild, NuGet, etc. will treat these the the same:
<PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0" />
<PackageReference Include="Contoso.Utility.UsefulStuff">
<Version>3.6.0</Version>
</PackageReference>
Same goes for the other attributes like "ExcludeAsset".
after using NuGet package manager (in VS), I ended up with quite different XML though.
The NuGet package manager in VS isn't honoring the original format of the document when it makes modifications to your <PackageReference>
's. It probably NuGet is getting the interpreted result of the .csproj file, not the raw XML, so it doesn't know whether you were using attributes or nested elements.