ASP.NET Web Forms web site assembly definition errors November 13, 2020

I have the unfortunate task of having to work with an ASP.NET Web Forms web site and have frequently had to deal with errors like the following:

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

This usually happens when switching from one branch to another in Git. Copying the correct assembly into the Bin directory didn’t help because it would just get replaced on build.

It turns out the cause was the .refresh files stored alongside the assembly .dll files in the Bin directory. These apparently get created when a reference is “added” to an ASP.NET web site. All they contain is a path to another copy of the relevant assembly, usually in your NuGet packages directory.

I’m not sure exactly what the purported benefit of .refresh files is, but they seem to cause more problems than they solve. The fact that there doesn’t seem to be any official documentation on the topic doesn’t help matters.

If you find yourself having the same issue and don’t want to do a package reinstall (which’ll update the references and therefore the .refresh files), you can either update the path in the .refresh file to point to the correct assembly version, or just delete the .refresh file as they aren’t a requirement.