Recently I learned about a pretty simple feature that is super useful when working with .NET solutions that have several
.csproj files, and the same NuGet package dependency in two or more of them.
The simple way to do this —what Visual Studio’s “Manage Nuget package” dialog does— is to add/update
PackageReference elements in each
csproj, like this:
But this means that whenever you want to update this dependency, you need to do it separately in each project… and I hope you don’t have too many of them.
The DRY way to do this, is with
Import elements in the
csproj file, that reference other (partial)
csproj files, like this:
Note that I used
.csproj.include for the shared
csproj file; the extension doesn’t actually matter.
Now whenever you need to update that dependency, you can do it in a single place and all the projects that reference that file will keep their versions in sync.
The only caveat I’ve found with this method is that Visual Studio’s “Manage Nuget packages” dialog doesn’t play well with it. If you use it in any particular project to update the package defined in the common file, a new
PackageReference will be added to that project file, and the
Import statement will remain. This won’t cause a build error, but depending on the order of the
Import elements in your project file, might end up causing one or other version of the package to be used. So make sure that your whole team understands how these shared dependencies packages need to be updated going forward.
Greaat read thankyou