Run*_*sen 5 c# assembly-binding-redirect anglesharp .net-4.7.2
我有一个旧版应用程序在旧的 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>
Run Code Online (Sandbox Code Playgroud)
这些是我可以从旧版本中识别出的唯一更改。
当我在生产中运行该应用程序时,它崩溃了。
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)
Run Code Online (Sandbox Code Playgroud)
在网上做了一些搜索之后,我看到有人指出了一个类似的问题(但是.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.
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
我的项目中不知道(对我而言)对 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.
Run Code Online (Sandbox Code Playgroud)
我确信这里一定有一些简单的重定向魔法。是否可以说服我的项目为 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>
Run Code Online (Sandbox Code Playgroud)
(我尝试用 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)
Run Code Online (Sandbox Code Playgroud)
所以这里显然存在一些版本不匹配,但坦率地说,我不知道这些版本号中哪些是重要的。
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
Run Code Online (Sandbox Code Playgroud)
这似乎表明它对次要版本不满意,但是......哪个?
AngleSharp 似乎要求 4.0.2.0?app.config 指的是 4.1.1.0 吗?4.6.26515.0 那是 dll 实际声称的版本?
在过去的一年里,我的团队遇到了各种绑定重定向问题,其中System.Net.Http、System.IO、 和System.Runtime是添加不正确重定向的罪魁祸首。这给我们带来了很多悲伤,但我找到了一套可以解决问题的步骤。
Get-Project \xe2\x80\x93All | Add-BindingRedirect该命令将为您的项目添加所需的绑定重定向,但不会删除未使用的重定向,这就是为什么我预先添加了删除所有重定向的第一步。
\n\n对于几个软件包来说,这似乎有点像大锤,但我很惊讶它从我们公认的旧解决方案中删除了多少垃圾,同时解决了我们遇到的问题。
\n这个问题就解决了。我无法完全解释解决方案,但我只是想将其放在这里,以防它可以帮助处于类似奇怪情况的其他人。
如前所述,运行环境是Win2008R2服务器。为 .Net 4.7.2 编译系统似乎一直工作正常,直到调用 AngleSharp(它又引入了 CodePages 引用)。
现在,在与负责 AngleSharp 项目的 Florian 交谈后,他担心 GAC 中可能潜伏着一些邪恶的东西。我搜索后,什么也没找到 - 但注意到服务器上确实没有安装.Net 4.7.2!我很快就发现了原因 - 这个古老的平台实际上不支持 4.6.2(?) 以外的版本。
这并不能解释为什么我自己的代码(为 4.7.2 编译)运行顺利,但一调用外部类库就崩溃了。不过,它可能确实向比我更了解 .NET 运行时的人解释了这一点。
所以我将项目设置为版本 4.6.2,进行编译,现在一切运行顺利。加载任何依赖项或任何东西都没有问题。
回想起来,这可能是显而易见的,但我坚持我的故事,因为我的可执行文件和至少一个类库正在为 4.7.2 编译并且仍在运行,所以我被骗了。
伙计们,请注意您的版本和平台支持。:|