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

.NetVisual Studio-2017.Net Standard.Net Standard-1.4

.Net Problem Overview


I have a .NET Standard 1.4 class library that references the System.ComponentModel.Annotations (4.3.0) NuGet package.

I'm then referencing this class library from a .NET Framework 4.6.2 test project. It builds fine, but at runtime I get the following error:

> System.IO.FileLoadException occurred HResult=0x80131040
> Message=Could not load file or assembly > 'System.ComponentModel.Annotations, Version=4.1.0.0, Culture=neutral, > PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The > located assembly's manifest definition does not match the assembly > reference. (Exception from HRESULT: 0x80131040)

I tried adding a reference to the System.ComponentModel.Annotations (4.3.0) NuGet package from the net462 project, but that didn't make any difference.

I tried adding a reference to the .NET Standard library from the net462 project, but still no luck.

Am I missing something here? Is this a known bug, if so is there a work around?

Any help is much appreciated!

.Net Solutions


Solution 1 - .Net

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:

https://stackoverflow.com/questions/43995432/could-not-load-file-or-assembly-microsoft-extensions-dependencyinjection-abstrac/43996389#43996389

https://stackoverflow.com/questions/44257436/when-using-net-standard-1-4-in-a-library-and-net-framework-4-6-1-in-and-applic

Solution 2 - .Net

I had similar problem but none of the above answers helped me. It turns out that solution is very easy, I've just run following command in Package Manager:

Install-Package System.ComponentModel.Annotations -Version 4.1.0

Solution 3 - .Net

In my case, I was using 4.0.0, so I fixed it by adding in

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.ComponentModel.Annotations"
                      publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="4.1.0.0" newVersion="4.0.0.0"/>
  </dependentAssembly>

Adapt to your required version.

Solution 4 - .Net

This usually happens when visual studio can't figure out the correct bindingRedirect.

Most likely the cause it that the version of the nugget does not match the version of the produced library.

To fix do this:

  1. From package manage console do:

    Get-Project –All | Add-BindingRedirect

    to regenerate assemblyBinding configuration at the config file

  2. If didn't fix it, then add manually the binding redirection:

     <dependentAssembly>
         <assemblyIdentity name="System.ComponentModel.Annotations"    publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
         <bindingRedirect oldVersion="0.0.0.0-X" newVersion="Y" />
     </dependentAssembly>
    

    where:

    1. X is the version that can't be load, from the error message
    2. Y is the version on your project references. To get it, select the library from the references node, and look for the version on property pane.

Solution 5 - .Net

Got it working by using assembly redirection as described in: just invoke FunctionsAssemblyResolver.RedirectAssembly() in the begining of your program. https://stackoverflow.com/a/50776946/2705777

using System.Reflection;
using System.Diagnostics;
using System.Linq;

public class FunctionsAssemblyResolver
{
    public static void RedirectAssembly()
    {
        var list = AppDomain.CurrentDomain.GetAssemblies().OrderByDescending(a => a.FullName).Select(a => a.FullName).ToList();
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
    }

    private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        var requestedAssembly = new AssemblyName(args.Name);
        Assembly assembly = null;
        AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
        try
        {
            assembly = Assembly.Load(requestedAssembly.Name);
        }
        catch (Exception ex)
        {
        }
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        return assembly;
    }

}

Solution 6 - .Net

Credit to @MiguelSlv as my case had to do with using a proper binding redirect. However, be sure that when you deploy your app to beta or production that the binding redirect is in your deployed web.config (not just in your development environment). I ended up using the following binding redirect for the NuGet package System.ComponentModel.Annotations 5.0.0 that is used in a .NET Standard 2.0 class project being consumed by my **ASP.NET MVC web application (.NET Framework 4.7.1)

<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
</dependentAssembly>

Note that 4.2.1.0 is the version shown under properties, while 5.0.0 is the NuGet package version.

Solution 7 - .Net

Fixed this by installing the same System.ComponentModel.Annotations version I want to use across all the projects in the solution.

Solution 8 - .Net

Please Add below dependentAssembly in your web.Config or app.Config file

The following configuration is added under the configuration --> runtime --> assemblyBinding node under:

<dependentAssembly>
        <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
      </dependentAssembly>

Solution 9 - .Net

For me, none of the other solutions worked.

I resolved this by manually adding a reference to System.ComponentModel.DataAnnotations myself (via project -> References), rather than letting Visual Studio handle it via the light-bulb quick-fix menu.

Solution 10 - .Net

Also for 4.2.0.0 version error this is fixed for me in web.config:

  <dependentAssembly>
    <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.6.0" />
  </dependentAssembly>
</assemblyBinding> 

Solution 11 - .Net

I fixed this error by doing the Clean Solution command in Visual Studio 2019.

Solution 12 - .Net

I have this issue by implementing a helper function redirecting the assembly at the begin (which was suggested in this answer):

public static class FunctionsAssemblyResolver
{
    #region Public Methods

    public static void RedirectAssembly()
    {
        AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblyOnCurrentDomain;
    }

    #endregion Public Methods

    #region Private Methods

    private static Assembly ResolveAssemblyOnCurrentDomain(object sender, ResolveEventArgs args)
    {
        var requestedAssembly = new AssemblyName(args.Name);
        var assembly = default(Assembly);

        AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssemblyOnCurrentDomain;

        try
        {
            assembly = Assembly.Load(requestedAssembly.Name);
        }
        catch
        { }

        AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblyOnCurrentDomain;

        return assembly;
    }

    #endregion Private Methods
}

Solution 13 - .Net

Linux Users

You may have to install mono if you're trying to generate .exe file from linux system.

See this link on how to install mono on ubuntu 20.04

Solution 14 - .Net

This is actually much easier than hacking config files. Hacking config files is a little scary; you might miss something! Let the IDE do it for you - hopefully it will do it right!

  1. Right click solution
  2. Click Manage Nuget Packages for Solution...
  3. Search for System.ComponentModel.Annotations
  4. Put a check box in your test projects and click Install

enter image description here

Now my unit tests run! Awesome!

It is still possible that you will need to use @Guillaume's answer if you have app.config files in your unit test project.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan EllisView Question on Stackoverflow
Solution 1 - .NetMartin UllrichView Answer on Stackoverflow
Solution 2 - .NetLukasz CokotView Answer on Stackoverflow
Solution 3 - .NetGuillaumeView Answer on Stackoverflow
Solution 4 - .NetMiguelSlvView Answer on Stackoverflow
Solution 5 - .NetNeilView Answer on Stackoverflow
Solution 6 - .NetsmooveyView Answer on Stackoverflow
Solution 7 - .Netuser14918236View Answer on Stackoverflow
Solution 8 - .NetShailesh KantariyaView Answer on Stackoverflow
Solution 9 - .NetRichView Answer on Stackoverflow
Solution 10 - .NettcetinView Answer on Stackoverflow
Solution 11 - .Netuser1605822View Answer on Stackoverflow
Solution 12 - .NetbudulView Answer on Stackoverflow
Solution 13 - .Net7guyoView Answer on Stackoverflow
Solution 14 - .NetJessView Answer on Stackoverflow