系統記憶體異常
當開發者在.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頁面。

