系統記憶體異常

This article was translated from English: Does it need improvement?
Translated
View the article in English

當開發人員在 .NET Framework 上使用 IronOCR 的最新版本時,有時執行程式會導致出現 System.Memory Exception 的錯誤,如下所示。

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

雖然此問題與 IronOCR 本身無直接關聯,但已知在 System.Memory 依賴項方面存在相容性挑戰。 即使已安裝 System.Memory 的新版本(例如 4.4.5 或更新版本)——無論是作為其他依賴項的一部分,還是直接引用——執行階段仍可能拋出與版本相關的錯誤。

這是因為某些依賴項強制執行嚴格的版本綁定,這意味著它們預期使用特定版本的 System.Memory,無論是否存在理論上相容的新版本。 因此,除非採用以下所述的解決方法,否則此錯誤將持續存在。

解決方案

解決此例外情況的方法是將 App.config 替換為以下程式碼。

 <runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
    </dependentAssembly>
    <dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
 <runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
    </dependentAssembly>
    <dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
XML

說明

修改後的 App.Config 旨在防止因多個依賴項需要同一組件的不同版本而可能產生的版本衝突。 透過此設定,應用程式將在執行時載入適當的版本。此重定向會強制所有元件使用版本 v4.0.1.2,從而消除前述例外情況發生的可能性。 修改 App.Config 後,請重新執行應用程式,以確保程式不會再引發其他例外狀況。

若需進一步了解此議題或參與討論,請造訪此處的 GitHub 頁面,該頁面詳述了此例外情況及目前的解決方法。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 5,896,332 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronOcr
執行範例 觀看您的圖片轉為可搜尋文字。