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.
This makes the mediator suitable for:
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.
// 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.
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 |
| 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 |
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.