Aspose.OCR C# .Net - Microsoft.ML.OnnxRuntime.NativeMethods Exception

Application works fine on our laptops but running on our VMWare windows 2019 and 2022 servers we have an exception occuring when executing:

         var result = ocr.Recognize(input);

Need help determing why it wont run on our VMware Aria Automation Windows Servers. I have included the entire procedure that does the OCR on the image.

    public PdfHeaderInfo ExtractHeader(string pdfPath)
    {
        if (string.IsNullOrWhiteSpace(pdfPath))
            throw new ArgumentException("PDF path is required.", nameof(pdfPath));

        if (!File.Exists(pdfPath))
            throw new FileNotFoundException("PDF file not found.", pdfPath);

        Log($"OCR starting for: {pdfPath}");

        var pdf = new Document(pdfPath);

        if (pdf.Pages.Count < 1)
            throw new Exception("PDF does not contain any pages.");

        Log($"PDF page count: {pdf.Pages.Count}");

        var device = new PngDevice(new Resolution(300));
        using var fullStream = new MemoryStream();
        device.Process(pdf.Pages[1], fullStream);
        fullStream.Position = 0;

        using var fullImage = DrawingImage.FromStream(fullStream);
        using var bitmap = new DrawingBitmap(fullImage);

        Log($"Rendered first page image size: {bitmap.Width}x{bitmap.Height}");

        // Save rendered page for debugging
        var debugDir = Path.GetDirectoryName(pdfPath) ?? Environment.CurrentDirectory;
        var debugImagePath = Path.Combine(
            debugDir,
            Path.GetFileNameWithoutExtension(pdfPath) + "_ocr_input.png");

        try
        {
            Directory.CreateDirectory(debugDir);
            bitmap.Save(debugImagePath, ImageFormat.Png);
            Log($"Saved OCR input image: {debugImagePath}");
        }
        catch (Exception ex)
        {
            Log($"WARNING: Could not save OCR input image: {ex.Message}");
        }

        using var imageStream = new MemoryStream();
        bitmap.Save(imageStream, ImageFormat.Png);
        imageStream.Position = 0;

        var ocr = new AsposeOcr();
        var input = new OcrInput(InputType.SingleImage);
        input.Add(imageStream);

        Log("Calling Aspose OCR...");
        var result = ocr.Recognize(input);

        var rawText = string.Join(Environment.NewLine, result.Select(r => r.RecognitionText));

        Log("OCR raw text:");
        Log(rawText);

        var header = ParseHeader(rawText);

        Log($"OCR parsed => DocumentNo='{header.DocumentNo}', Revision='{header.Revision}', CageCode='{header.CageCode}', PartNumber='{header.PartNumber}'");

        return header;
    }

Below is the exception we are getting.

[2026-06-22 13:53:07] [OCR] Calling Aspose OCR…
[2026-06-22 13:53:09] [2026-06-22 13:53:09] [828d5d9e3dc948edb39d1162c80f7a4f_EM-FA-K1933_Rev_NEW] OCR fallback failed: System.TypeInitializationException: The type initializer for ‘Microsoft.ML.OnnxRuntime.NativeMethods’ threw an exception.
—> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.ML.OnnxRuntime.NativeMethods…cctor()
— End of inner exception stack trace —
at Microsoft.ML.OnnxRuntime.SessionOptions…ctor()
at :clubs:↨↨.:slight_smile:()
at :clubs:↨↨.:clubs:.:slight_smile:()
at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy1.CreateValue() at System.Lazy1.get_Value()
at :clubs:↨↨.:slight_smile:(DetectAreasMode :slight_smile:, Func`1)
at :clubs:↨↨.:slight_smile:(DetectAreasMode :slight_smile:)
at ☼§→…ctor(RecognitionSettings :slight_smile:, ☼←, :clubs:↨↨ :clubs:)
at Aspose.OCR.AsposeOcr.Recognize(OcrInput images, RecognitionSettings settings)
at Aspose.OCR.AsposeOcr.Recognize(OcrInput images)
at AssemblyInstructionsExtract.Services.AssemblyInstructionOcrService.ExtractHeader(String pdfPath) in \AssemblyInstructionsExtract\Services\ExtractionService.cs:line 77

I have attached a screen shot of our available servers available C++ redistributable.
Screenshot 2026-06-22 140001.png (104.1 KB)

It works just fine on our development laptops, it is not running in our production server environment.

Here is the problem i think, Nuget install associates 1.22.1 with aspose.OCR version 26.04. According to your AI bot its only been tested with 1.18 and it stated that it could not confirm it would work with 1.22.1.

So i down graded to 25.01 which had a referecnce to OnnxRuntime of 1.18, it still failed because Nuget was still using the 1.22.1 transative dll for the project.

I manually download version 1.18 and copied it into the install directory and it now runs.

This to me seems to be a problem, i want to use the lastest OCR with the lastest OnnxRuntime. Suggestions on how to remain current and use NuGet to keep packages correct.

What this means:

.NET C# Aspose.OCR version 26.04 expects a specific ONNX runtime version 1.18
but NuGet is resolving a different ONNX version transitively 1.22.1
and the app only works when the 1.18 actual runtime binaries on disk 1.18 match what the OCR package expects

Again it works without issues on my Windows 11 laptop running from Visual Studio 2022 but not on the Windows Server 2022 or 2019 unless i overwrite the ONNX to version 1.18.

@david_j_meyer2_boeing_com
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): OCRNET-1245

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

There is an error in our current external dependencies documentation. It still lists Microsoft.ML.OnnxRuntime 1.18.0, but that is outdated.

Aspose.OCR for .NET 25.7.0 and later use Microsoft.ML.OnnxRuntime 1.22.1. Aspose.OCR 26.4/26.5 are expected to work with ONNX Runtime 1.22.1.

Since ONNX Runtime 1.18.x works on your Windows Server but 1.22.1 fails, this does not look like an OCR recognition issue. It points to ONNX Runtime native initialization on that server.

The screenshot shows that a VC++ v14 Redistributable is installed, so this may not be a completely missing VC++ runtime. However, ONNX Runtime 1.22.1 may require newer native runtime components than the older ONNX version. Please repair/reinstall the latest Microsoft Visual C++ Redistributable x64 on the affected server.

Also please verify that the deployed application folder and PATH do not contain older copies of:

  • onnxruntime.dll
  • vcruntime140.dll
  • vcruntime140_1.dll
  • msvcp140.dll

Please also confirm the application is running as x64 and that Prefer 32-bit is disabled.

Its all servers and even the onces with the latest version Microsoft Visual C++ Redistributable x64 on the affected server.