Dan*_*lis 40 .net visual-studio-2017 .net-standard .net-standard-1.4
我有一个.NET Standard 1.4类库,它引用了System.ComponentModel.Annotations(4.3.0)NuGet包.
然后,我将从.NET Framework 4.6.2测试项目中引用此类库.它构建良好,但在运行时我收到以下错误:
发生System.IO.FileLoadException HResult = 0x80131040
消息= 无法加载文件或程序集'System.ComponentModel.Annotations,Version = 4.1.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(HRESULT异常:0x80131040)
我尝试从net462项目添加对System.ComponentModel.Annotations(4.3.0)NuGet包的引用,但这没有任何区别.
我尝试从net462项目中添加对.NET标准库的引用,但仍然没有运气.
我在这里错过了什么吗?这是一个已知的错误,如果有的话有解决方法吗?
任何帮助深表感谢!
Mar*_*ich 40
在许多情况下,可以通过添加以下测试项目的csproj文件来解决这个问题:
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
这会强制构建过程.dll.config在输出目录中创建具有所需绑定重定向的文件.
原因是"经典"csproj测试项目是真正的"库",并且默认情况下不被认为需要绑定重定向.但运行单元测试需要这样做.如果引用的项目需要这些重定向才能正常工作,这只会成为一个问题.这通常适用于直接安装引用库使用的所有NuGet包,但是使用新PackageReference样式的NuGet包时,它不会.
查看此修复程序有帮助的其他实例:
无法加载文件或程序集Microsoft.Extensions.DependencyInjection.Abstractions,Version = 1.1.0.0
在库和.Net framework 4.6.1中使用.Net Standard 1.4和应用程序时,无法加载文件System.IO.FileSystem,Version = 4.0.1.0
小智 18
我有类似的问题,但上述答案都没有帮助我.事实证明解决方案非常简单,我只是在包管理器中运行以下命令:
Install-Package System.ComponentModel.Annotations -Version 4.1.0
Gui*_*ume 10
就我而言,我使用的是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>
Run Code Online (Sandbox Code Playgroud)
适应您所需的版本.
Tan*_*pps 10
感谢@MiguelSlv,因为我的案例与使用正确的绑定重定向有关。但是,请确保当您将应用程序部署到测试版或生产环境时,绑定重定向位于您部署的 web.config 中(而不仅仅是在您的开发环境中)。我最终对 NuGet 包System.ComponentModel.Annotations 5.0.0使用以下绑定重定向,该包在我的 **ASP.NET MVC Web 应用程序(.NET Framework 4.7.1)使用的.NET Standard 2.0类项目中使用)
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)
请注意,4.2.1.0 是属性下显示的版本,而 5.0.0 是 NuGet 包版本。
这通常发生在 Visual Studio 无法找出正确的 bindingRedirect 时。
很可能是因为 nugget 的版本与生成的库的版本不匹配。
要修复这样做:
从包管理控制台执行:
Get-Project –All | Add-BindingRedirect
assemblyBinding在配置文件中重新生成配置
如果没有修复它,则手动添加绑定重定向:
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-X" newVersion="Y" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)
在哪里:
| 归档时间: |
|
| 查看次数: |
25912 次 |
| 最近记录: |