Could not load file or assembly ‘System.ComponentModel.Annotations, Version=4.1.0.0

In many cases, this can be solved by adding the following code to the csproj file of your test project:

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

This forces the build process to create a .dll.config file in the output directory with the needed binding redirects.

The reason is that “classic” csproj test projects are true “libraries” and are not considered to need binding redirects by default. But running unit tests requires this. This only becomes an issue if referenced projects need those redirects to work correctly. This usually works when directly installing all NuGet packages that the referenced library uses, but with the new PackageReference style of NuGet packages, it does not.

See other instances where this fix has helped:

Could not load file or assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0

When using .Net Standard 1.4 in a library and .Net framework 4.6.1 in and application, unable to load file System.IO.FileSystem, Version=4.0.1.0

Leave a Comment