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

前端 未结 9 1233
情书的邮戳
情书的邮戳 2020-12-01 09:13

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

相关标签:
9条回答
  • 2020-12-01 09:27

    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.

    0 讨论(0)
  • 2020-12-01 09:34

    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.

    0 讨论(0)
  • 2020-12-01 09:35

    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

    0 讨论(0)
  • 2020-12-01 09:36

    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;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-01 09:37

    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> 
    
    0 讨论(0)
  • 2020-12-01 09:39

    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
    }
    
    0 讨论(0)
提交回复
热议问题