Preview of ahead of time compilation in ASP.NET Core shows big gains, big compatibility issues

Preview of ahead of time compilation in ASP.NET Core shows big gains, big compatibility issues

Microsoft has released preview 3 of .NET 8, the forthcoming LTS (Long term support) release expected in November this year, with key new features including AOT (ahead of time) compilation in ASP.NET Core.

AOT compilation means the application is compiled to native code before it is deployed, as opposed to JIT (Just In Time) compilation which is invoked at runtime. The advantage is not so much computational performance, but smaller footprint and faster start-up. Fast start-up is critical in serverless environments and also important in containerized environments where new instances are deployed for scalability or application updates.

According to Program Manager Daniel Roth, the new preview tested on a simple application reduced application size by 87 percent and startup time by 80 percent.

Performance gains in ASP.NET AOT are primarily to do with size, memory and startup time, rather than raw speed

Unfortunately, there are substantial trade-offs. Dynamic loading of .NET assemblies and the ability to generate code at runtime are important features used by some libraries and frameworks, which means that ASP.NET Core AOT has limited compatibility. Features which are not yet supported include the ASP.NET MVC (Model View Controller) framework, the Blazor framework, the SignalR real-time communications library, and ASP.NET Authentication. Which of these gaps may be filled by the time of full release is not yet clear, though Roth stated that JWT (JSON web token) authentication and ADO.NET drivers for SQLite and PostgreSQL will be in future previews.

Workarounds for library developers or developers fixing their own code include source code generation at compile time, and the .NET team has built helper tools into Roslyn, the .NET compiler platform; but converting existing libraries to be compatible with .NET AOT is not trivial. Any code which does serialization to or from JSON (JavaScript Object Notation) must be adapted to use the JSON source generator, for example.

ORM (object-relational mapping) frameworks typically make use of dynamic features of .NET. Microsoft’s Entity Framework Core team is working on AOT support. The popular alternative Dapper has an AOT project which is currently not receiving much attention.

The reality is that even in November, many applications will not be able to take advantage of ASP.NET Core AOT – but making it available will give developers and the wider ecosystem an incentive to make code compatible.

Another key update in the latest previews is server-side rendering for Blazor components, part of an effort to unify Blazor to work both client-side and server-side. Also new is the ability to render Razor components outside the context of an HTTP request, for example for static site generation.