[Public] Fixing System.Reflection.TargetInvocationException when using IronOCR with System.Memory version conflict
A version conflict with System.Memory can cause IronOCR to crash during OCR operations. Resolving the version mismatch through a binding redirect can fix the issue.
If you’re encountering the following exception when using IronTesseract().Read()
in IronOCR:
System.Reflection.TargetInvocationException (mscorlib.dll)
"Exception has been thrown by the target of an invocation"
and receiving empty OCR results before the crash, this is likely due to a version conflict between different System.Memory
assemblies.
This issue often occurs even in clean projects and may be accompanied by a warning similar to:
Found conflicts between different versions of "System.Memory"...
"System.Memory, Version=4.0.1.2" was chosen because it was primary and "System.Memory, Version=4.0.2.0" was not.
Solution
To resolve the crash, you need to explicitly add a binding redirect in your application’s App.config
file. This will force your application to consistently use the version of System.Memory
expected by the IronOCR library.
Here’s what to add:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="4.0.2.0" newVersion="4.0.1.2" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
This ensures your application loads version 4.0.1.2 of System.Memory
, preventing the crash and restoring correct OCR behavior.