【问题标题】:Could not load file or assembly 'System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies无法加载文件或程序集 'System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一
【发布时间】:2018-08-21 08:19:36
【问题描述】:

在过去的几天里,我一直在拼命尝试解决一个在安装 NuGet 包后陷入的依赖地狱的情况,该包添加了来自System.Collections.*System.Diagnostics.*System.IO.*System.Runtime.*System.Text.*System.Threading.*System.Xml.* 命名空间。 MVC 项目我目前在目标 .Net 4.7.1 中遇到问题。

软件包安装将以下绑定重定向添加到我的 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" />

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

我很难理解这里发生了什么。例如,如果我在 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>

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

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

【问题讨论】:

    标签: c# .net visual-studio web-config nuget


    【解决方案1】:

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

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

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

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

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

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 2017-07-02
      • 2017-05-12
      • 2017-02-14
      • 2017-11-08
      • 2018-03-21
      • 2016-07-19
      相关资源
      最近更新 更多