With Native AOT the application is compiled to machine code at publish time. No installed runtime is needed, startup is almost instant and memory usage drops: ideal for container services, serverless functions and command-line tools.
Enabling it
<PropertyGroup>
<PublishAot>true</PublishAot>
</PropertyGroup>
dotnet publish -c Release -r linux-x64
What changes in the code
The AOT compiler must know in advance all the code that will run. That is why dynamic reflection, loading assemblies at runtime and generating code on the fly are restricted. The compiler reports this with specific warnings: take them seriously.
For JSON serialisation you use compile-time code generation:
[JsonSerializable(typeof(Order))]
[JsonSerializable(typeof(List<Order>))]
internal partial class AppJsonContext : JsonSerializerContext { }
var json = JsonSerializer.Serialize(order, AppJsonContext.Default.Order);
ASP.NET Core and AOT
Minimal APIs support Native AOT; the dedicated template uses WebApplication.CreateSlimBuilder, which includes only the essential services. MVC and Razor Pages, on the other hand, are not AOT compatible.
When not to use it
Applications with plugins loaded at runtime, ORMs or reflection-based libraries that are not compatible, or projects where startup time is not an issue: in those cases traditional publishing remains the simpler choice.
Comments (0)
No comments yet.