Telling Visual Studio not to use latest packages

When your C# projects are referencing NuGet packages, you are always encouraged to update to latest version.
This can be annoying, when you really don’t want to.

Edit: This is a general advise for NuGet package version dependencies, even though the example is about SDK for Dynamics CRM.

In the world of Microsoft Dynamics 365/CE/CRM, you may have to use SDK of previous major version (8.x), while Visual Studio still wants you to use the latest available packages (9.x).

But there IS a way to tell Visual Studio to only suggest updates for the major version your code supports.

 

Background

This is a common packages.config file in a Dynamics 365 8.2 project:

<packages>
  <package id="Microsoft.CrmSdk.CoreAssemblies" version="8.2.0" targetFramework="net462" />
  <package id="Microsoft.IdentityModel" version="6.1.7600.16394" targetFramework="net462" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.29.0" targetFramework="net462" />
  <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net462" />
</packages>

This setup gives me two recommendations for updates:

Visual Studio NuGet packages

And every time I check my packages, I will be encouraged to update these.

 

Telling Visual Studio what you want

By manually editing the packages.config file, you can tell VS to ignore versions based on a wildcard pattern.

Adding the attribute allowedVersions lets you specify for example which Major versions you are interested in for this project.

<packages>
  <package allowedVersions="[8,9)" id="Microsoft.CrmSdk.CoreAssemblies" version="8.2.0" targetFramework="net462" />
  <package id="Microsoft.IdentityModel" version="6.1.7600.16394" targetFramework="net462" />
  <package allowedVersions="[2,3)" id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.29.0" targetFramework="net462" />
  <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net462" />
</packages>

The example above states that my project accepts:
– versions 8 and above, but below 9
of Microsoft.CrmSdk.CoreAssemblies
– versions 2 and above, but below 3
of Microsoft.IdentityModel.Clients.ActiveDirectory.

The syntax is not really like anything I have ever seen before, but following the documentation on docs.microsoft.com shows you what is possible.

Now the suggested updates I am presented with are only the ones I actually want.

We can see in the image above that latest version of this package is v9.0.2.4, but when checking which version is actually suggested, it will be the latest with Major version 8, as instructed in the packages.config file.

And versions with Major other than 8 are displayed as Blocked by package.config.

Looking at Installed packages, I still see the latest versions available on NuGet, but they are not listed as available Updates.

 

Conclusion

Many times you miss information of relevant updates, as you get used to ignore proposed updates to packages that you do not want in your project.

Adding the allowedVersions attribute in the packages.config file lets you trust the suggestions, which will help you keep your code up to date.

One thought on “Telling Visual Studio not to use latest packages

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.