首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在导入AngleSharp时,在.Net 4.7.2中导入NetStandard 2时,以某种方式尝试将NetStandard 2作为dll导入

在导入AngleSharp时,在.Net 4.7.2中导入NetStandard 2时,以某种方式尝试将NetStandard 2作为dll导入
EN

Stack Overflow用户
提问于 2019-05-12 18:29:04
回答 2查看 2.4K关注 0票数 6

我有一个旧的Windows2008 R2服务器上运行的遗留应用程序。该应用程序已经通过了许多.Net版本,目前是4.7.2。它作为一块岩石已经稳定了多年,虽然有迁移计划要离开旧的物理服务器,但它必须活一段时间。

一个新的特性看到了一些HTML抓取的必要性。我为NuGet v0.12.0添加了AngleSharp包,进行了单元测试,并且在我的Win10开发机器上都做得很好。使用AngleSharp的新类只起作用。

因此,我部署到prod服务器,包括对app.config的更改;

代码语言:javascript
复制
  <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>

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

当我在生产中运行这个应用程序时,它会崩溃。

代码语言:javascript
复制
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核心,但仍然相似),他们指出,尝试调用Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);可能会带来不同的结果。参考资料:https://github.com/DiscUtils/DiscUtils/issues/77

对我来说不是,但现在我收到了更多古怪的信息:

代码语言:javascript
复制
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.

网标是怎么进来的?我不知道。我还在绑定日志中看到了这一点,感谢Splats异常日志记录:

代码语言:javascript
复制
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 Framework4.5、4.6.x或4.7.x项目。

我还遇到了一个似乎与此相关的问题:https://github.com/dotnet/announcements/issues/31

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

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

代码语言:javascript
复制
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.

我肯定这里一定有一些简单的重定向魔法。是否有可能说服我的项目为NETFrameworkv4.7.2加载它,并且只工作呢?

谢谢你的建议!

编辑5月14日:对我来说最混乱的地方是所有不同的版本号。

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

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

如果我右键单击放入bin\Debug文件夹中的实际DLL,选择Properties,然后选择Version,它将报告File和product 4.6.26515。

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

代码语言:javascript
复制
    <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的两个实例,只是为了好玩,但没有改变)

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

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
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实际上声称的版本吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-20 08:43:30

这个问题解决了。我不能完全解释这个解决方案,但我只想把它放在这里,以防它能帮助其他人在类似的奇怪情况下。

如前所述,运行时环境是一个Win2008R2服务器。为.Net 4.7.2编译系统似乎很好,一直到AngleSharp被调用为止(然后它又被CodePages引用)。

现在,在与负责AngleSharp项目的弗洛里安交谈后,他担心GAC中可能隐藏着一些邪恶的东西。我搜索了一下,什么也没找到--但注意到.Net 4.7.2确实没有安装在服务器上!我很快就发现了原因--除了4.6.2(?)在这个古老的平台上得到了支持。

这并不能解释为什么我自己的代码--为4.7.2编译的代码--在没有问题的情况下运行,而是在调用外部类库时立即崩溃。不过,它可能确实向比我更了解.NET运行时的人解释了这一点。

因此,我将项目设置为4.6.2版本,编译后,现在一切都很顺利。加载任何依赖项或其他任何内容都没有问题。

回想起来,这可能是显而易见的,但我坚持我的故事,因为我的可执行文件和至少一个类库被编译为4.7.2并且仍然在运行。

注意你的版本和平台支持,伙计们。:

票数 3
EN

Stack Overflow用户

发布于 2019-05-12 18:44:26

在过去的一年中,我的团队遇到了各种各样的绑定重定向问题,其中System.Net.HttpSystem.IOSystem.Runtime是添加不正确重定向的主要原因。这给我们带来了很大的悲痛,但我找到了一套为我们解决问题的步骤。

  1. 从所有项目中删除所有绑定重定向
  2. 打开包管理器控制台
  3. 运行Get-Project –All | Add-BindingRedirect

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

这似乎有点像一个大锤的几个包,但我惊讶地发现,它从我们的,公认的旧,解决办法,而解决我们的问题的垃圾如此之多。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56102231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档