為網站寫了自動測試專案,其中引用 PuppeteerSharp 模擬網頁操作。同事由版控取回專案編譯時噴出以下錯誤:

System.IO.FileLoadException: '無法載入檔案或組件 
'System.Runtime.InteropServices.RuntimeInformation, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 
或其相依性的其中之一。 找到的組件資訊清單定義與組件參考不符。 (發生例外狀況於 HRESULT: 0x80131040)'
 於 PuppeteerSharp.BrowserFetcher.GetCurrentPlatform()
 於 PuppeteerSharp.BrowserFetcher..ctor()

專案的參照組件清單並無 System.Runtime.InteropServices.RuntimeInformation,猜想是 PuppeteerSharp 間接引用的。初步調查,PuppeteerSharp 依賴 .NET Standard 2.0,起初我專案選 4.5.2 安裝 PuppeteerSharp 失敗,後來一口氣改成 4.7.2 就好了,故猜想問題可能與 .NET Framework 版本有關。

同事提供線索,說當初開啟專案時 VS2019 提示專案版本為 .NET 4.7.2,詢問要升級還是降版。由此查出同事的機器還沒安裝 4.7.2,只裝到 4.6。抓到方向,問題果然在升級 4.7.1 後消失。

但問題來了,莫非用 .NET Standard 2.0 的專案一定要選 .NET 4.7.2?依據官方文件,.NET 4.6.1 就符合 .NET Standard 2.0,實測將專案改成 4.6.1 也能正常執行 PuppeteerSharp。但文件上有段附註:

While NuGet considers .NET Framework 4.6.1 as supporting .NET Standard 1.5 through 2.0, there are several issues with consuming .NET Standard libraries that were built for those versions from .NET Framework 4.6.1 projects. For .NET Framework projects that need to use such libraries, we recommend that you upgrade the project to target .NET Framework 4.7.2 or higher.

大意是雖然 .NET 4.6.1 被視為支援 .NET Standard 1.5/2.0,但 4.6.1 建置出來元件在使用上可能會遇到問題,如果要使用這類元件,建議將專案調成 .NET Framework 4.7.2+。

在網路上找到幾則類似問題都指向 .NET Standard 2.0 + 4.6.x 的組合是關鍵:

結論:遇 .NET Standard 2.0 + .NET 4.6.x 出現 System.Runtime.InteropServices.RuntimeInformation 等型別無法載入檔案或組件,且指定版號為 0.0.0.0 時,請先嘗試升級 4.7.2。

A case of System.Runtime.InteropServices.RuntimeInformation TypeLoadException issue on referencing .NET Standard 2.0 library in .NET 4.6 project.


Comments

Be the first to post a comment

Post a comment