我有一个旧版应用程序在旧的 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
它不适合我,但现在我收到了更多时髦的信息:
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.6.26515.0 替换 4.1.1.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 实际声称的版本?