本应兼容的不兼容 .NET Standard 程序集? [英] Incompatible .NET Standard assemblies that should have been compatible?

查看:31
本文介绍了本应兼容的不兼容 .NET Standard 程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a .NET Standard 2.0 DLL project.

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

It has no other references, apart from NETStandard.Library 2.0.1 and all is fine.

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

It is referenced by a WPF application and everything seems to work fine.

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

Once I add a nuget package for System.Collections.Immutable 1.5.0 to the DLL project, a yellow exclamation mark appears on the Dependencies root of the Solution Explorer. (Hm... I now see that the exclamation remains even after I remove this package, but I guess this is a VS bug since everything seemed to work just fine before adding this library)

拥有这两个包,一切都很好,但是当我尝试使用 ImmutableDictionary 时,我得到了这个:

Having both packages, everything builds fine, but when I try to use the ImmutableDictionary, I get this:

System.IO.FileNotFoundException:无法加载文件或程序集System.Collections.Immutable,版本=1.2.3.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"或其依赖项之一.系统找不到指定的文件.'

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.

Looking at the nuget packages, I understand that those two should work together, since (from my understanding) the 'System.Collections.Immutable` library says it supports .NET standard 2.0.

我对 .NET Standard 世界很陌生.

I am very new to the .NET Standard world.

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

Looking at the public key token of the System.Collections.Immutable.dll in my main app's bin folder I see that it was indeed b03f5f7f11d50a3a as expected. However the version is different.

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

Normally, in the old .NET I would try to add an assembly redirection in the app's config file. But doing this here does not seem to make any difference. Adding this into my WPF app that references the DLL:

  <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>

... 将异常更改为:

... changes the exception to this:

System.IO.FileNotFoundException:无法加载文件或程序集System.Collections.Immutable,版本=4.0.11.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"或其依赖项之一.系统找不到指定的文件.'

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.'

内部异常

FileNotFoundException:无法加载文件或程序集System.Collections.Immutable,Version=1.2.3.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"或其依赖项之一.系统找不到指定的文件.

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 包吗?

What is going on here? Can't I use those two nuget packages together?

更新:

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

I now notice that the DLL version that I got was from System.Collections.Concurrent.dll, while I am interested at System.Collections.Immutable.dll which is nowhere to be found on my bin folders... I haven't even found where the nuget packages are stored in my solution.

更新 2:

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

It turns out that I get the exact same behavior when I create a new solution with similar setup (Full framework console app referencing .NET Standard 2 DLL).

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

Following @Lasse Vågsæther Karlsen's advice, I have downloaded the nuget package from here https://www.nuget.org/packages/System.Collections.Immutable, extracted the correct DLL out of it, and placed it in my bin folder. Then the console app worked.

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

I am using the 15.7.1 version of Visual Studio. I am not sure what are the changes are of this being fixed in the latest 15.7.2 version...

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

You can try my verifiable example by downloading it from here: https://mega.nz/#!YZMwGABD!eHHxvNdO59l2m5ZlHMG1vvqCd-GHv3EVckR-9htDojs

推荐答案

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

There are 2 ways to fix this:

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

1 - Update the project file for the ConsoleApp1 to include this in the property group

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

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

2 - Add a reference to System.Collections.Immutable directly in the ConsoleApp1 project.

这是一个已知问题,即使用项目到项目引用和 NuGet 包引用不会自动正确地传递引用.正在此处跟踪此问题,但其中一种解决方法应该可以解除阻止.

This is a known issue where using Project-to-Project references and NuGet package references does not automatically flow the references correctly. This is being tracked here but one of these workarounds should unblock this.

这篇关于本应兼容的不兼容 .NET Standard 程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆