系統記憶體異常

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擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

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

準備好開始了嗎?
Nuget 下載 5,299,091 | 版本: 2025.12 剛剛發布