系統記憶體異常
當開發人員在 .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>
說明
修改後的 App.Config 旨在防止因多個依賴項需要同一組件的不同版本而可能產生的版本衝突。 透過此設定,應用程式將在執行時載入適當的版本。此重定向會強制所有元件使用版本 v4.0.1.2,從而消除前述例外情況發生的可能性。 修改 App.Config 後,請重新執行應用程式,以確保程式不會再引發其他例外狀況。
若需進一步了解此議題或參與討論,請造訪此處的 GitHub 頁面,該頁面詳述了此例外情況及目前的解決方法。

