Could not load file or assembly #39;System.ComponentModel.Annotations, Version=4.1.0.0(无法加载文件或程序集System.ComponentModel.Annotations,版本 = 4.1.0.0)
问题描述
我有一个引用 System.ComponentModel.Annotations (4.3.0) NuGet 包的 .NET Standard 1.4 类库.
I have a .NET Standard 1.4 class library that references the System.ComponentModel.Annotations (4.3.0) NuGet package.
然后我从 .NET Framework 4.6.2 测试项目中引用这个类库.它构建良好,但在运行时出现以下错误:
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 发生 HResult=0x80131040
Message=无法加载文件或程序集'System.ComponentModel.Annotations,版本=4.1.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一.这定位程序集的清单定义与程序集不匹配参考.(HRESULT 异常:0x80131040)
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)
我尝试从 net462 项目中添加对 System.ComponentModel.Annotations (4.3.0) NuGet 包的引用,但这没有任何区别.
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 项目中添加对 .NET Standard 库的引用,但仍然没有成功.
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?
非常感谢任何帮助!
推荐答案
在很多情况下,可以通过在你的测试项目的csproj文件中加入如下代码来解决:
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>
这会强制构建过程在输出目录中创建一个带有所需绑定重定向的 .dll.config
文件.
This forces the build process to create a .dll.config
file in the output directory with the needed binding redirects.
原因是经典";csproj 测试项目是真正的库";并且默认情况下不被认为需要绑定重定向.但是运行单元测试需要这个.如果引用的项目需要这些重定向才能正常工作,这只会成为一个问题.这通常在直接安装引用的库使用的所有 NuGet 包时有效,但对于 NuGet 包的新 PackageReference
样式,它不会.
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:
无法加载文件或程序集 Microsoft.Extensions.DependencyInjection.Abstractions,版本=1.1.0.0
在库中使用 .Net Standard 1.4 和在应用程序中使用 .Net framework 4.6.1 时,无法加载文件 System.IO.FileSystem, Version=4.0.1.0
这篇关于无法加载文件或程序集'System.ComponentModel.Annotations,版本 = 4.1.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法加载文件或程序集'System.ComponentModel.Annotations,版本 = 4.1.0.0
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01