在 .Net 4.7.2 中导入 AngleSharp 时绑定重定向以某种方式尝试将 NetStandard 2 作为 dll 引入

2023-12-29

我有一个旧版应用程序在旧的 Windows 2008 R2 服务器上运行。该应用程序已经经历了多个.Net版本,目前为4.7.2。它多年来一直坚如磐石,虽然有迁移计划从旧的物理服务器上迁移,但它必须再活一段时间。

一项新功能发现需要进行一些 HTML 抓取。我添加了 AngleSharp v0.12.0 的 NuGet 包,经过单元测试,在我的 Win10 开发机器上一切正常。使用 AngleSharp 的新类很简单。

所以我部署到了prod服务器,包括app.config的修改;

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Text.Encoding.CodePages" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

这些是我可以从旧版本中识别出的唯一更改。

当我在生产中运行该应用程序时,它崩溃了。

System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

在网上做了一些搜索后,我看到有人指出了类似的问题(.Net Core 然而,但仍然相似),他们指出尝试调用Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);可能会有所作为。参考:https://github.com/DiscUtils/DiscUtils/issues/77 https://github.com/DiscUtils/DiscUtils/issues/77

这不适合我,但现在我收到了更多时髦的消息:

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

netstandard是如何涉足这一领域的?我不知道。我还在 Splats 异常日志记录的绑定日志中看到了这一点:

5496 === Pre-bind state information ===
5497 LOG: DisplayName = netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
5498  (Fully-specified)
5499 LOG: Appbase = file:///E:/dcollect/Ingest/
5500 LOG: Initial PrivatePath = NULL
5501 Calling assembly : System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
5502 ===
5503 LOG: This bind starts in default load context.
5504 LOG: Using application configuration file: E:\dcollect\Ingest\Ingest.exe.Config
5505 LOG: Using host configuration file:
5506 LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
5507 LOG: Post-policy reference: netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
5508 LOG: Attempting download of new URL file:///E:/dcollect/Ingest/netstandard.DLL.
5509 LOG: Attempting download of new URL file:///E:/dcollect/Ingest/netstandard/netstandard.DLL.
5510 LOG: Attempting download of new URL file:///E:/dcollect/Ingest/netstandard.EXE.
5511 LOG: Attempting download of new URL file:///E:/dcollect/Ingest/netstandard/netstandard.EXE.

我的项目中没有已知的(对我而言)对 Netstandard 的引用,所有项目都是 .Net Framework 4.5、4.6.x 或 4.7.x 项目。

我还发现了这个似乎可能相关的:https://github.com/dotnet/announcements/issues/31

我尝试将我的项目转换为 PackageReference 项目,但这也没有改变任何内容。

为了完整起见,这里是我最初尝试使用 AngleSharp 的绑定日志。

5576 [20:24:01.522] <ERROR> EventParser: Exception caught: System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
5577 File name: 'System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
5578    at AngleSharp.Browser.EncodingMetaHandler..ctor()
5579    at AngleSharp.Configuration..ctor(IEnumerable`1 services)
5580    at AngleSharp.Configuration.get_Default()
5581    at Teller.Charts.DataCollection.EventParser.ParseHTML(String html)
5582 === Pre-bind state information ===
5583 LOG: DisplayName = System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
5584  (Fully-specified)
5585 LOG: Appbase = file:///E:/dcollect/Ingest/
5586 LOG: Initial PrivatePath = NULL
5587 Calling assembly : AngleSharp, Version=0.11.0.0, Culture=neutral, PublicKeyToken=e83494dcdc6d31ea.
5588 ===
5589 LOG: This bind starts in default load context.
5590 LOG: Using application configuration file: E:\dcollect\Ingest\Ingest.exe.Config
5591 LOG: Using host configuration file:
5592 LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
5593 LOG: Post-policy reference: System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
5594 LOG: Attempting download of new URL file:///E:/dcollect/Ingest/System.Text.Encoding.CodePages.DLL.
5595 WRN: Comparing the assembly name resulted in the mismatch: Minor Version
5596 ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

我确信我在这里缺少一些简单的重定向魔法。是否可以说服我的项目为 Net Framework v4.7.2 加载它并正常工作?

感谢您的任何建议!

5月14日编辑: 对我来说最困惑的一点是所涉及的所有不同版本号。

AngleSharp 0.12.0 NuGet 包依赖于 System.Text.Encoding.CodePages >= 4.5.0。

因此,即使 v4.5.1 存在,它也会自动安装 v4.5.0,因为依赖性行为最低。

如果我右键单击放入 bin\Debug 文件夹中的实际 DLL,选择“属性”,然后选择“版本”,它会报告文件和产品版本 4.6.26515。

但在我的 app.config 中,我得到的绑定重定向指的是 4.1.1.0。

    <dependentAssembly>
      <assemblyIdentity name="System.Text.Encoding.CodePages" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
    </dependentAssembly>

(我尝试将 4.1.1.0 的两个实例替换为 4.6.26515.0 只是为了好玩,但这没有任何改变)

然后,如上所述,日志声称:

29014 [22:42:44.735] <ERROR> EventInfoCreator: Failed to parse: System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
29015 File name: 'System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
29016    at AngleSharp.Browser.EncodingMetaHandler..ctor()
29017    at AngleSharp.Configuration..ctor(IEnumerable`1 services)
29018    at AngleSharp.BrowsingContext.New(IConfiguration configuration)
29019    at Teller.Charts.DataCollection.EventParser.ParseHTML(String html)
29020    at Teller.Charts.DataCollection.EventParser.ParseFile(String filePath)
29021    at Teller.Charts.DataCollection.EventInfoCreator.CreateEventInfoProps(String html, String storageDirectory)

因此,这里显然存在一些版本不匹配的情况,但坦率地说,我不知道哪些版本号是重要的。

29034 LOG: Attempting download of new URL file:///E:/dcollect/Ingest/System.Text.Encoding.CodePages.DLL.
29035 WRN: Comparing the assembly name resulted in the mismatch: Minor Version

这似乎表明它对小版本不满意,但是……是哪一个?

AngleSharp 似乎要求 4.0.2.0? app.config 指的是 4.1.1.0? 4.6.26515.0 是 dll 实际声称的版本吗?


在过去的一年里,我的团队遇到了各种绑定重定向问题,其中System.Net.Http, System.IO, and System.Runtime是添加错误重定向的罪魁祸首。这给我们带来了很多悲伤,但我找到了一套可以解决问题的步骤。

  1. Delete all绑定来自所有项目的重定向
  2. 打开包管理器控制台
  3. Run Get-Project –All | Add-BindingRedirect

该命令将为您的项目添加所需的绑定重定向,但不会删除未使用的重定向,这就是为什么我预先添加了删除所有重定向的第一步。

对于几个软件包来说,这似乎有点像大锤,但我很惊讶它从我们公认的旧解决方案中删除了多少垃圾,同时解决了我们遇到的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 .Net 4.7.2 中导入 AngleSharp 时绑定重定向以某种方式尝试将 NetStandard 2 作为 dll 引入 的相关文章

随机推荐