.net 无法加载文件或程序集“System.ComponentModel.Annotations,版本 = 4.1.0.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44053187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.1.0.0
提问by Dan Ellis
I have a .NET Standard 1.4 class library that references the System.ComponentModel.Annotations (4.3.0) NuGet package.
我有一个引用 System.ComponentModel.Annotations (4.3.0) NuGet 包的 .NET Standard 1.4 类库。
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:
然后我从 .NET Framework 4.6.2 测试项目中引用这个类库。它构建良好,但在运行时出现以下错误:
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)
System.IO.FileLoadException 发生 HResult=0x80131040
消息=无法加载文件或程序集“System.ComponentModel.Annotations,版本=4.1.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 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.
我尝试从 net462 项目中添加对 System.ComponentModel.Annotations (4.3.0) NuGet 包的引用,但这没有任何区别。
I tried adding a reference to the .NET Standard library from the net462 project, but still no luck.
我尝试从 net462 项目中添加对 .NET Standard 库的引用,但仍然没有运气。
Am I missing something here? Is this a known bug, if so is there a work around?
我在这里错过了什么吗?这是一个已知的错误,如果是,是否有解决方法?
Any help is much appreciated!
任何帮助深表感谢!
回答by Martin Ullrich
In many cases, this can be solved by adding the following the the csproj file of your test project:
在许多情况下,这可以通过在测试项目的 csproj 文件中添加以下内容来解决:
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
This forces the build process to create a .dll.configfile in the output directory with the needed binding redirects.
这会强制构建过程.dll.config在输出目录中创建一个具有所需绑定重定向的文件。
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 PackageReferencestyle of NuGet packages, it does not.
原因是“经典”csproj 测试项目是真正的“库”,默认情况下不需要绑定重定向。但是运行单元测试需要这个。如果引用的项目需要这些重定向才能正常工作,这只会成为一个问题。这通常在直接安装引用的库使用的所有 NuGet 包时有效,但对于新PackageReference风格的 NuGet 包,则不行。
See other instances where this fix has helped:
查看此修复程序有帮助的其他实例:
无法加载文件或程序集 Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0
回答by Lukasz Cokot
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
安装包 System.ComponentModel.Annotations -Version 4.1.0
回答by Guillaume
In my case, I was using 4.0.0, so I fixed it by adding in
就我而言,我使用的是 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>
Adapt to your required version.
适应您所需的版本。
回答by Neil
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
通过使用程序集重定向来使其工作,如中所述:只需FunctionsAssemblyResolver.RedirectAssembly()在程序开始时调用。
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;
}
}
回答by MiguelSlv
This usually happens when visual studio can't figure out the correct bindingRedirect.
这通常发生在 Visual Studio 无法找出正确的 bindingRedirect 时。
Most likely the cause it that the version of the nugget does not match the version of the produced library.
很可能是因为 nugget 的版本与生成的库的版本不匹配。
To fix do this:
要修复这样做:
From package manage console do:
Get-Project –All | Add-BindingRedirectto regenerate
assemblyBindingconfiguration at the config fileIf 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:
- X is the version that can't be load, from the error message
- 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.
从包管理控制台执行:
Get-Project –All | Add-BindingRedirectassemblyBinding在配置文件中重新生成配置如果没有修复它,则手动添加绑定重定向:
<dependentAssembly> <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-X" newVersion="Y" /> </dependentAssembly>在哪里:
- X 是无法加载的版本,来自错误信息
- Y 是您的项目参考中的版本。要获取它,请从引用节点中选择库,然后在属性窗格中查找版本。
回答by user1605822
I fixed this error by doing the Clean Solution command in Visual Studio 2019.
我通过在 Visual Studio 2019 中执行 Clean Solution 命令修复了此错误。
回答by kodcu
Also for 4.2.0.0version error this is fixed for me in web.config:
同样对于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>
回答by Rich
For me, none of the other solutions worked.
对我来说,其他解决方案都没有奏效。
I resolved this by manually adding a reference to System.ComponentModel.DataAnnotationsmyself (via project -> References), rather than letting Visual Studio handle it via the light-bulb quick-fix menu.
我通过手动添加对System.ComponentModel.DataAnnotations自己的引用(通过项目 -> 引用)解决了这个问题,而不是让 Visual Studio 通过灯泡快速修复菜单处理它。
回答by budul
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
}

