DSoftStudio Mediator

← Back to Documentation

Native AOT and Trimming

DSoftStudio.Mediator is fully compatible with .NET Native AOT publishing and IL trimming.

Both packages ship with IsAotCompatible and IsTrimmable enabled, and the trim analyzer is active at build time. The hot execution path uses no reflection, no MakeGenericType, no Expression.Compile, and no dynamic method generation — all handler discovery and dispatch wiring are performed at compile time by Roslyn source generators.

Use Cases

This makes the mediator suitable for:

AOT-Safe Runtime Dispatch

The Publish(object) and Send(object) overloads (runtime-typed dispatch) are also AOT-safe — they use compile-time generated FrozenDictionary<Type, DispatchDelegate> dispatch tables populated by the source generator, with no MakeGenericType at runtime.

Publishing a Native AOT Application

// Program.cs — Minimal API with Native AOT
var builder = WebApplication.CreateSlimBuilder(args);

builder.Services
    .AddMediator()
    .RegisterMediatorHandlers()
    .PrecompilePipelines()
    .PrecompileNotifications()
    .PrecompileStreams();

var app = builder.Build();
app.Services.ValidateMediatorHandlers();

app.MapPost("/ping", async (IMediator mediator) =>
    await mediator.Send(new Ping()));

app.Run();

Publish with:

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

No trimming warnings, no reflection fallbacks, no rd.xml configuration needed.

Cold Start Performance

Native AOT eliminates JIT warm-up entirely. Combined with PrecompilePipelines(), the mediator is ready to dispatch on the very first request:

Metric JIT (.NET 10) Native AOT
Cold start (mediator init) 1.62 µs < 1 µs (no JIT)
First Send() call Same as warm Same as warm
Binary size (self-contained) ~80 MB ~15-25 MB

What Makes It AOT-Compatible

Technique Why it matters for AOT
Source generators (not reflection) No Type.GetType(), no Assembly.GetTypes()
FrozenDictionary<Type, Delegate> Pre-built dispatch tables, no MakeGenericType
Interface dispatch (not delegates) No Expression.Compile(), no DynamicMethod
ValueTask<T> returns No Task allocator dependency
Static generic specialization CLR creates dispatch tables per-type at compile time

Trimming

Both DSoftStudio.Mediator and DSoftStudio.Mediator.Abstractions ship with:

<IsTrimmable>true</IsTrimmable>
<IsAotCompatible>true</IsAotCompatible>

The ILLink trim analyzer runs at build time. If your handlers reference types that are not trim-safe, you’ll get standard IL2xxx warnings — but the mediator infrastructure itself produces zero trimming warnings.

See Also