系統記憶體異常:解決IronOCR OCR辨識中的 System.Memory 錯誤
當開發人員在.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頁面,其中詳細介紹了異常和當前的解決方法。

