系統記憶體異常

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大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 6,151,372 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

想要快速證明? PM > Install-Package IronOcr
執行範例 觀看您的圖像轉變為可搜尋文字。