首页 > 解决方案 > 无法加载文件或程序集 'System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一

问题描述

在过去的几天里,我一直在拼命地试图解决一个我在安装 NuGet 包后遇到的依赖问题,该包从System.Collections.*System.Diagnostics.*System.IO.*System.Runtime.*、和命名空间System.Text.*中添加了一堆新的依赖项。MVC 项目我目前在目标 .Net 4.7.1 中遇到问题。System.Threading.*System.Xml.*

软件包安装将以下绑定重定向添加到我的 web.config

  <dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.IO" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Runtime.Extensions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Linq" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Xml.ReaderWriter" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Linq.Expressions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
  </dependentAssembly>

…导致以下异常:

Could not load file or assembly 'System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

web.config 中突出显示的行是:

<add assembly="System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

我最终发现,如果我删除了 bindingRedirect,System.IO它会清除该错误,然后抱怨绑定重定向列表中的其他项目之一。我一一删除了所有绑定重定向,问题就解决了。

我很难理解这里发生了什么。例如,如果我在 Visual Studio 项目的引用中查看 System.IO 的属性检查器,它指向C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\Facades\System.IO.dll而不是 Nuget 文件夹,如果我在另一个编辑器中打开 .csproj 文件,我可以看到引用实际上,看起来如下:

<Reference Include="System.IO, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
  <HintPath>..\packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
  <Private>True</Private>
  <Private>True</Private>
</Reference>

这里肯定有问题,因为有重复的<Private>True</Private>条目,我已经更正了一次,但它们被添加回来。这是所有新组件的问题。我想知道某个地方的 Nuget 包是否存在问题?

任何帮助/建议将不胜感激。

标签: c#.netvisual-studioweb-confignuget

解决方案


我想知道某个地方的 Nuget 包是否存在问题?

是的,这应该是 nuget 包的问题。有些nuget包有依赖NETStandard.Library,它会拉入那些system.*依赖,另一方面,一些nuget包在.NET Standard框架下安装依赖不正确,我什至在这里这里都报告过,这个问题与nuget有关包本身,我们需要联系这个包的作者来更新这个包。

以上可能是您在安装添加了一堆system.*依赖项的 NuGet 包后进入的原因。同时,.NET Framework 4.7.1 增加了对 .NET Standard in-box 的支持,我们不需要将那些 system.* nuget 包添加到包中。当其他 nuget 包拉入这些system.*依赖包时,nuget/VS 会为那些来自 nuget 的包添加绑定重定向到 web.config 文件,但 VSsystem.*默认使用来自 .net 框架的那些,在这种情况下,VS 可能找不到那些system.*由于绑定重定向的依赖关系。这就是您收到错误“无法加载文件或程序集'System.IO ...”的原因。

要解决此问题,解决方法是从程序集的 web.config 文件中删除绑定重定向。检查 .NET Framework 4.7.1 上的线程.NET Standard 问题以了解一些详细信息。

此外,如果您不想将这些系统依赖项添加到您的项目中,您可以将它们从您的项目中删除。

希望这可以帮助。


推荐阅读