Failed to load file or assembly "System.ComponentModel.Annotations, Version = 4.1.0.0

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

Then I refer to this class library from the .NET Framework 4.6.2 test project. It builds fine, but at runtime I get the following error:

Event System.IO.FileLoadException HResult = 0x80131040 Failed
Message = Failed to load file or assembly 'System.ComponentModel.Annotations, Version = 4.1.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a' or one of its dependencies. The installed assembly manifest definition does not match the Help assembly. (Exception from HRESULT: 0x80131040)

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

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

Am I missing something? Is this a known bug if it works?

Any help is much appreciated!

+37
source share
8 answers

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

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

This forces the build process to create a file .dll.configin the output directory with the necessary link redirects.

, "" csproj "" . . , , , . NuGet, , , PackageReference NuGet, .

, :

Microsoft.Extensions.DependencyInjection.Abstractions, Version = 1.1.0.0

.Net Standard 1.4 .Net framework 4.6.1 System.IO.FileSystem, Version = 4.0.1.0

+37

, . , , :

Install-Package System.ComponentModel.Annotations -Version 4.1.0

+17

4.0.0, ,

<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>

.

+10

, , : FunctionsAssemblyResolver.RedirectAssembly() . fooobar.com/questions/15685624/...

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;
    }

}
+2

.

, System.ComponentModel.DataAnnotations ( project → References), , Visual Studio .

0

4.2.0.0 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> 
0

, " " Visual Studio 2019.

0

Source: https://habr.com/ru/post/1017358/