【问题标题】:Nuget dll hell : How to solve "Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.5.4.0"Nuget dll地狱:如何解决“无法加载文件或程序集'System.Threading.Tasks.Extensions,版本= 4.5.4.0”
【发布时间】:2021-08-27 08:20:56
【问题描述】:

我有一个引用 dll 项目的测试项目;此 dll 项目引用自身 Azure Nugets,尤其是 Azure.Storage.Blobs (v12.9.1) 到达此指令时,我的一项测试失败了:

var blobServiceClient = new BlobServiceClient(connectionString);

例外是:

System.IO.FileLoadException
  HResult=0x80131040
  Message=Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.5.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
  Source=Azure.Storage.Blobs
  StackTrace:
   at Azure.Storage.Blobs.BlobServiceClient..ctor(String connectionString, BlobClientOptions options)
   at Azure.Storage.Blobs.BlobServiceClient..ctor(String connectionString)
  (…)

Inner Exception 1:
FileLoadException: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

我想了解并解决这个异常。

注意事项:

  • UnitTest 项目正在使用 .Net Framework 4.7.2
  • 引用的项目使用 .Net 标准 2.0,并引用其他项目。
  • System.Threading.Tasks.Extensions 既没有被我的测试项目显式引用,也没有被引用的 dll 引用。

我已经调查过的内容:

  1. 在测试项目 App.Config 中添加绑定重定向:
          <dependentAssembly>
            <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.5.4.0" newVersion="4.5.4.0" />
          </dependentAssembly>

注意:在我接下来的调查步骤中,我保留了这个绑定重定向

  1. 检查 System.Threading.Tasks.Extensions.dll 是否存在于测试项目 bin/debug 文件夹中 => 确实如此,这个 dll 存在。注意:我不知道如何确定它的 nuget 版本(windows explorer 中的详细信息仅给出 dll 版本)

  2. 向单元测试项目添加对 nuget System.Threading.Tasks.Extensions v4.5.4.0(最新版本)的引用

  3. 添加对 nuget System.Threading.Tasks.Extensions v4.5.4.0(最新版本)的引用到引用的项目

感谢您的帮助。

【问题讨论】:

  • 尝试将bindingRedirect更改为oldVersion="0.0.0.0-4.5.4"newVersion="4.5.4",看看是否可行。如果不是,请尝试将其更改为oldVersion="0.0.0.0-4.2.0.1"newVersion="4.2.0.1"。另外,尝试清除 NuGet 缓存(工具 > NuGet 包管理器 > 包管理器设置 > 清除所有 NuGet 缓存)。
  • 应用 oldVersion="0.0.0.0-4.2.0.1", newVersion="4.2.0.1" 解决了这个问题。谢谢天宇。这对我来说没有意义,因为 4.2 不是我安装的 nuget 版本。为什么是小版本“.1”?
  • 有些区别,4.2.0.1是System.Threading.Task.ExtensionsNuGet包的最新汇编版本,4.5.4是NuGet包版本。通常汇编版本用于 元素。请参阅此文档:<bindingRedirect> Element。无论如何,很高兴知道您的问题已解决。

标签: c# visual-studio dll nuget visual-studio-2019


【解决方案1】:

既然解决了你的问题,我做了一个总结,希望对其他遇到类似/相同问题的论坛成员也有帮助。

对于元素,请考虑使用程序集版本,而不是 NuGet 包版本。详情请见here

对于System.Threading.Task.ExtensionsNuGet包,目前4.5.4是最新的NuGet包版本,4.2.0.1是最新的汇编版本,所以尽量使用4.2.0.1。

【讨论】:

    猜你喜欢
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    相关资源
    最近更新 更多