c# - 本应兼容的不兼容 .NET Standard 程序集?

标签 c# .net .net-assembly .net-standard-2.0 assembly-binding-redirect

我有一个 .NET Standard 2.0 DLL 项目。

Project target framework

NETStandard.Library 2.0.1 外,没有其他引用一切都很好。

它被 WPF 应用程序引用,一切似乎都工作正常。

一旦我为 System.Collections.Immutable 1.5.0 添加了一个 nuget 包DLL工程,Dependencies上出现黄色感叹号解决方案资源管理器的根目录。 (嗯......我现在看到即使在我删除这个包之后感叹号仍然存在,但我猜这是一个 VS 错误,因为在添加这个库之前一切似乎都工作得很好)

exclamation without explanation...

有了这两个包,一切都很好,但是当我尝试使用 ImmutableDictionary ,我明白了:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Collections.Immutable, Version=1.2.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

查看 nuget 包,我知道这两者应该一起工作,因为(根据我的理解)“System.Collections.Immutable”库表示它支持 .NET 标准 2.0。

packages

我对 .NET Standard 世界还很陌生。

查看 System.Collections.Immutable.dll 的公钥 token 在我的主应用程序中 bin文件夹我看到它确实是b03f5f7f11d50a3a正如预期的那样。但是版本不同。

Hm...

通常,在旧的 .NET 中,我会尝试在应用程序的配置文件中添加程序集重定向。但是在这里这样做似乎没有任何区别。将其添加到我引用 DLL 的 WPF 应用程序中:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.11.0" newVersion="4.0.11.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

... 将异常更改为:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Collections.Immutable, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

Inner Exception

FileNotFoundException: Could not load file or assembly 'System.Collections.Immutable, Version=1.2.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

这是怎么回事? 我不能同时使用这两个 nuget 包吗?

更新:

我现在注意到我得到的 DLL 版本来自 System.Collections.Concurrent.dll ,虽然我对 System.Collections.Immutable.dll 感兴趣在我的 bin 文件夹中找不到它...... 我什至没有找到 nuget 包在我的解决方案中的存储位置。

更新 2:

事实证明,当我创建具有类似设置的新解决方案(引用 .NET Standard 2 DLL 的完整框架控制台应用程序)时,我得到了完全相同的行为。

按照@Lasse Vågsæther Karlsen 的建议,我从这里下载了 nuget 包 https://www.nuget.org/packages/System.Collections.Immutable ,从中提取正确的 DLL,并将其放在我的 bin 文件夹中。然后控制台应用程序运行。

我使用的是 15.7.1 版的 Visual Studio。我不确定在最新的 15.7.2 版本中修复了哪些变化...

您可以从这里下载我的可验证示例: https://mega.nz/#!YZMwGABD!eHHxvNdO59l2m5ZlHMG1vvqCd-GHv3EVckR-9htDojs

最佳答案

有两种方法可以解决这个问题:

1 - 更新 ConsoleApp1 的项目文件以将其包含在属性组中

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

2 - 直接在 ConsoleApp1 中添加对 System.Collections.Immutable 的引用 项目。

这是一个已知问题,使用项目到项目引用和 NuGet 包引用不会自动正确地传递引用。这正在被追踪here但这些解决方法之一应该可以解除阻止。

关于c# - 本应兼容的不兼容 .NET Standard 程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50771569/

相关文章:

c# - 在 C# 4.0 中具有持续运行线程的 Windows 服务的最佳解决方案

C#:构建后在程序集 list 中声明了哪些 Dll

c# - 用 Mono 编译 IronPython

c# - 串流阅读器问题

C# 将位图加载到 PictureBox 中 - 速度慢并且会阻塞 UI ..还有其他选择吗?

c# - CLR 是否仍然在进程中加载​​程序集,即使它具有该程序集的 Ngen 副本

c# - "Could not load file or assembly ' System.Core, Version=2.0.5.0,...”动态加载可移植类库时出现异常

c# - 我想让 EditorFor 文本框只接受数字

c# - 如何修剪字符之间的空格

.net - .NET 中 variable = 0 和 variable = nothing 有什么区别?