系统内存异常

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 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 5,556,263 | 版本: 2026.3 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronOcr
运行示例 观看您的图像变成可搜索文本。