Skip to content

27x Native AOT and Trimming

Peter Gill edited this page Oct 10, 2026 · 7 revisions

27.x only (the main branch, not yet released).

Native AOT and trimming (27.x)

Majorsilence.Reporting.RdlEngine, RdlCri, RdlCmd, RdlNative, PdfNative and the Majorsilence.Pdf* libraries are built with the AOT and trim analyzers on, and RdlEngine builds with no AOT or trim warnings on net8.0 and net10.0. Anything that cannot work ahead of time fails with a PlatformNotSupportedException that says what to register instead, rather than a missing-method error at some later render.

The repository publishes a report-rendering test as a real Native AOT binary on Linux, macOS and Windows in CI (Examples/RdlAotSmokeTest), so these claims are exercised, not just analyzed.

What works under AOT

Feature Notes
Built-in expression functions Math, Convert, String, Financial and the VB functions (Left, Format, IIf, ...).
Tables, lists, matrices, charts, groups, aggregates, subreports
Output formats PDF (including encryption and signing), HTML, XML, CSV, RTF, Word, MHTML, Excel, TIFF.
Databases SQLite, SQL Server, PostgreSQL, MySQL through RegisterDataProvider (see below).
Pushing data DataSet.SetData<T>(IEnumerable<T>), DataSet.SetCollectionData(...), SetData(DataTable), SetData(IDataReader), SetData(XmlDocument).
Static helper classes in expressions Register with RdlEngineConfig.RegisterType.
Instance helper classes (<Classes>) Register with RegisterType and RegisterInstanceFactory.
Custom report items (barcodes, QR codes, your own) Register with RegisterCustomReportItem<T>(name); every barcode type shipped in RdlCri is tested.
Custom code in expressions Register a code provider with RdlEngineConfig.RegisterCodeProvider.

The CI smoke test renders built-in functions, registered static and instance helpers, a registered custom report item, every way of pushing data (SetData<T>, SetCollectionData, DataTable, IDataReader, XmlDocument), groups and aggregates, a matrix, a list, a chart, a subreport, a SQLite query, and the PDF, HTML, XML, CSV, RTF, Word, MHTML, Excel (three variants) and TIFF renderers, all ten barcode and QR types, and PDF encryption and digital signing applied by the engine.

Call every Register... method before RdlEngineConfigInit() and before parsing a report.

RdlEngineConfig.RegisterType("MyApp.Helpers", typeof(Helpers));
RdlEngineConfig.RegisterType("MyApp.Greeter", typeof(Greeter));
RdlEngineConfig.RegisterInstanceFactory("MyApp.Greeter", () => new Greeter());
RdlEngineConfig.RegisterCustomReportItem<Majorsilence.Reporting.Cri.QrCode>("QRCode");
RdlEngineConfig.RdlEngineConfigInit();

var parser = new RDLParser(rdlText) { Folder = reportFolder, SkipDatabaseSchemaValidation = true };
using var report = await parser.Parse();
await report.DataSets["Data"].SetData(rows);          // IEnumerable<T>: members of T are kept by the trimmer
await report.RunGetData();
await report.RunRender(new OneFileStreamGen("out.pdf", true), OutputPresentationType.PDF);

SetData<T> matches the public fields and properties declared on T. The older SetData(IEnumerable, bool) reflects over each item's runtime type, so it is marked [RequiresUnreferencedCode]; use SetData<T> or SetCollectionData in trimmed and AOT apps.

What does not work under AOT

Feature Why Use instead
<CodeModules> (loading an assembly named in the report) Assemblies cannot be loaded at runtime RegisterType / RegisterInstanceFactory
VB <Code> blocks The VB source is compiled at runtime Precompile at build time, RegisterCodeProvider, or move the logic into a registered class
Data providers, custom report items or compression modules named in RdlEngineConfig.xml They are loaded from assemblies at runtime RegisterDataProvider, RegisterCustomReportItem; compression falls back to the built-in Deflate
XSLT data transforms XslCompiledTransform generates IL Transform the data before pushing it
The WSDL web service data provider Generates a proxy assembly at runtime Call the service in your code and push the data

Each of these throws PlatformNotSupportedException under AOT with a message naming the replacement. RdlDoctor reports <Code>, <CodeModules> and the WebService provider (rule DOC011), so reports can be checked ahead of time.

Precompiling <Code> blocks (opt-in)

A report's <Code> block is VB source that arrives at runtime. Native AOT has no JIT and no VB compiler, so by default the block is rejected with a report error. The opt-in RdlCodeGen build task moves the compilation to build time, where the VB compiler is available.

Warning: full VB is not supported by the AOT runtime. Only the <Code> text of the reports you list at build time is compiled. A report loaded later with different code fails. Late binding (Option Strict Off calls on Object) and reflection inside the code fail under AOT. ByRef, Optional and ParamArray methods are skipped, and Public Const/Public Dim members are not reachable from expressions. The build prints warning RDLCODE100 every time until you set RdlPrecompileAcknowledgeLimits to true. Treat the report files as trusted source: the build compiles whatever VB they contain.

How it works:

  1. A small Visual Basic class library references the Majorsilence.Reporting.AotCode NuGet package (the MSBuild targets and the generator tool; it imports itself) and lists the reports. It also references the RdlEngine:

    <ItemGroup>
      <PackageReference Include="Majorsilence.Reporting.AotCode" Version="27.*" PrivateAssets="all" />
    </ItemGroup>
    <PropertyGroup>
      <RdlPrecompileCode>true</RdlPrecompileCode>
      <RootNamespace>ReportCode</RootNamespace>
    </PropertyGroup>
    <ItemGroup>
      <RdlReports Include="../Reports/**/*.rdl" />
    </ItemGroup>
  2. Before compilation, RdlCodeGen reads each report's <Code>, wraps it in the same class the runtime compiler would generate, and writes RdlPrecompiledCode.g.vb. For every public method it also writes a direct-call dispatcher (no reflection), so the trimmer keeps the code.

  3. The VB compiler builds that file; your C# app references the library and calls ReportCode.RdlPrecompiledCode.Register() once at startup, before parsing.

  4. When a report is parsed, the engine hashes its <Code> text (ignoring line endings and surrounding whitespace) and uses the matching precompiled block. A block that was not precompiled gives the report error ...requires VB.NET compilation... (code hash xxxx), naming the hash to look for.

The same block works without AOT, so the report is unchanged. The repo example is Examples/AotPrecompiledCode (a ReportCode.vbproj plus an AOT console app; inside the repo it imports the targets from source instead of the package). To write the functions by hand instead, see Examples/AotCodeProvider.

Database drivers

The config file finds SQL drivers (Microsoft.Data.SqlClient, Microsoft.Data.Sqlite, Npgsql, MySql.Data, ...) by loading their assemblies from disk, which Native AOT cannot do. Register each driver your app uses with RdlEngineConfig.RegisterDataProvider, using the name reports put in <DataProvider>:

RdlEngineConfig.RegisterDataProvider("Microsoft.Data.Sqlite",
    cs => new Microsoft.Data.Sqlite.SqliteConnection(cs),
    tableSelect: "SELECT name FROM sqlite_master WHERE type = 'table'");
RdlEngineConfig.RegisterDataProvider("Microsoft.Data.SqlClient", cs => new Microsoft.Data.SqlClient.SqlConnection(cs));
RdlEngineConfig.RegisterDataProvider("PostgreSQL", cs => new Npgsql.NpgsqlConnection(cs));
RdlEngineConfig.RegisterDataProvider("Oracle.ManagedDataAccess", cs => new Oracle.ManagedDataAccess.Client.OracleConnection(cs));
RdlEngineConfig.RegisterDataProvider("MySQL.NET", cs => new MySqlConnector.MySqlConnection(cs),
    tableSelect: "show tables", replaceParameters: true);

Call it before RdlEngineConfigInit(). A registered provider takes precedence over a config-file entry of the same name, and it also tells RdlDoctor the provider exists. RdlCmd and RdlNative register these drivers themselves when they run as a Native AOT binary; in a normal .NET run they keep using the config file unchanged.

Driver Under Native AOT
SQLite (Microsoft.Data.Sqlite) Works.
SQL Server (Microsoft.Data.SqlClient) Works, with one condition: it does not support InvariantGlobalization, so leave that off.
PostgreSQL (Npgsql) Works.
Oracle (Oracle.ManagedDataAccess.Core) Works. Register it as Oracle.ManagedDataAccess; the AOT RdlCmd and RdlNative do.
MySQL, Oracle's MySql.Data Does not work. It can create a connection but fails to open one: its static initializer uses System.Configuration, which Native AOT cannot run.
MySQL, MySqlConnector Works. The AOT RdlCmd and RdlNative serve the MySQL.NET provider name with MySqlConnector, so existing MySQL reports run unchanged; in your own app register MySqlConnector.MySqlConnection as shown above.

CI proves this by running the smoke test as a Native AOT binary against real PostgreSQL, MySQL, Oracle and SQL Server servers (service containers, aot-databases workflow), and against a SQLite file in the normal AOT job. The repository also runs the own test suites of the Python, Ruby, PHP and Rust wrappers against the published AOT rdlnative library (the linux-build job checks out each wrapper's default branch), so the native-library route is covered end to end for all four languages.

The built-in providers in Majorsilence.Reporting.DataProviders (JSON, XML, text, file-directory, log, Gedcom, iTunes) are analyzed and warning-free; only the WSDL web service provider is unsupported (see above).

Publishing

dotnet publish -c Release -r linux-x64 -p:PublishAot=true --self-contained true

RdlCmd and the RdlNative / PdfNative shared libraries are published this way in the release builds (build-release-aot-*.ps1).

Clone this wiki locally