Could not load file or assembly 'System.Compon

2020-01-30 23:54发布

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!

8条回答
劫难
2楼-- · 2020-01-30 23:58

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.

查看更多
你好瞎i
3楼-- · 2020-01-31 00:04

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.

查看更多
地球回转人心会变
4楼-- · 2020-01-31 00:06

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

查看更多
虎瘦雄心在
5楼-- · 2020-01-31 00:08

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.
查看更多
淡お忘
6楼-- · 2020-01-31 00:09

In many cases, this can be solved by adding the following the 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

查看更多
仙女界的扛把子
7楼-- · 2020-01-31 00:09

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> 
查看更多
登录 后发表回答