So, we keep seeing issues on different projects with the error:
Could not load file or assembly ‘System.Buffers, Version=4.0.x.x
The error can sometimes seem intermittent, or just plain baffling. If you then examine the /BIN directory, and look at the properties (right click on the dll, and look at the file properties) of System.Buffers, you’ll find that it’s a wrong version; it’s not what is referenced in web.config (remembering that you may have an Assembly binding redirect in place) at all.
I’m tired of digging out details for myself/my colleagues, so here’s what’s happening…
Sitecore have given us a NuGet feed of packages for their assemblies. Those nuget packages are versioned to match the different versions of Sitecore. So far, so simple.
However, it appears that different Sitecore nuget packages can reference different versions of some of their dependencies – in particular, System.Buffers. For example, Sitecore.Kernel might reference System.Buffers version 22.214.171.124, while Sitecore.ExperienceEditor might reference version 126.96.36.199. (This is a contrived example; I can’t be bothered to dig through nugets and find a real-world example).
This means that the version of System.Buffers published becomes a “last-wins” situation, and that publishing a particular project from Visual Studio (rather than the whole solution) can result in a mismatch of the System.Buffers version.
One might reasonably wonder why a single version of Sitecore might have matching nuget packages referencing different versions of an assembly as standard; I believe this is an error or oversight.
Anyway, options to “fix” this:
- Only publish the entire solution each time. This would at least result in consistent results.
- Change your publishing order to make sure that the correct version “wins” in a solution build, and be aware that if publishing a single project, you may sometimes need to publish a second project to correct the first.
- Add a reference directly to the correct version of System.Buffers to the last project in your build order. In a Helix solution, this would be something in the Project tier.
- Exclude System.Buffers from your deployment, and use what is installed with Sitecore (though you might have to add an assembly binding redirect for that.)
Also be aware the version in the Assembly strong name can be wildly different to the actual version of the System.Buffers assembly.