.net-8.0onnxonnxruntime

Is ONNX Runtime compatible with AOT in .NET 8, and if not, is it supported in .NET 9?


I'm currently working on a .NET 8 project with the following setup:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <Version>1.0.1</Version>
        <Company>E. Corp</Company>
        <Product>malware-detector</Product>
        <Description>Malware detection tool</Description>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="NumSharp" Version="0.30.0" />
        <PackageReference Include="Microsoft.ML" Version="4.0.0" />
        <PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.20.1" />
        <PackageReference Include="Microsoft.ML.OnnxConverter" Version="0.22.0" />
        <PackageReference Include="Microsoft.ML.OnnxTransformer" Version="4.0.0" />
        <PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
        <None Update="model/malware_model.onnx">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>

        <PackageReference Include="Serilog" Version="4.2.0" />
        <PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
        <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
    </ItemGroup>

    <PropertyGroup>
        <PublishAot>true</PublishAot>
        <IsAotCompatible>true</IsAotCompatible>
        <SelfContained>true</SelfContained>
        <DebugType>none</DebugType>
        <DebugSymbols>false</DebugSymbols>
        <StackTraceSupport>false</StackTraceSupport>
        <StripSymbols>true</StripSymbols>
        <GenerateDocumentationFile>false</GenerateDocumentationFile>
        <Optimize>true</Optimize>
        <OptimizationPreference>Speed</OptimizationPreference>
    </PropertyGroup>
</Project>

When I attempt to run my application, I encounter the following error:

Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.PlatformNotSupportedException: Dynamic code generation is not supported on this platform.
   at System.Reflection.Emit.AssemblyBuilder.ThrowDynamicCodeNotSupported()
   ...

It seems that some part of the library (likely ONNX Runtime or ML.NET) is attempting to use Reflection.Emit or similar features that are incompatible with AOT.

My Questions:

  1. Is Microsoft.ML.OnnxRuntime fully compatible with AOT in .NET 8?
  2. If not, is AOT support improved in .NET 9 for ONNX Runtime?

I've found these resources but could not find a clear answer:

Any insights or workarounds would be greatly appreciated. Thanks!


Solution

  • It seems that as of March 2025, ML.NET has not been made compatible with AOT compilation, and there hasn't been any effort to change that. However, if you're primarily interested in inference, you can still leverage ONNX Runtime, which does support AOT.