Native AOT Deployment Model in dotNet

Funda Özmen
adessoTurkey
Published in
6 min readAug 7, 2023
Native AOT deployment in .Net
Native AOT deployment in .Net

Are you interested in learning about the native Ahead of Time (AOT) in .NET? According to the Microsoft Build team, native AOT can shrink app sizes and boost performance. But is that really the case? What are its limitations, and how can you utilize native AOT for deployment?

Let’s embark on this exploration together. In this article, we dig into the technical aspects of the native AOT deployment model and its relationship with the .NET framework.

What is Native AOT deployment?

The native AOT deployment model employs an ahead-of-time (AOT) compiler to compile IL (intermediate or half-compiled code) during the publication process. Unlike JIT (just-in-time) compilation, AOT apps do not rely on a JIT compiler during runtime. Instead, the code is compiled in advance, allowing native AOT apps to function in restricted environments where a JIT compiler is not permitted.

What are the Requirements?

To utilize the Native AOT deployment model, certain requirements need to be met:

Windows: Install Visual Studio 2022, ensuring that the “Desktop development with C++” workload is selected along with all default components. This provides the required tools, libraries, and components for compiling and building native code. While the Native AOT deployment model primarily focuses on compiling .NET code to native machine code, it may rely on underlying C++ components or libraries during the compilation process.

Visual Studio Installer page

Linux: Install the necessary compiler toolchain and developer packages for the libraries that the .NET runtime relies on.

macOS: Install the latest Command Line Tools for XCode, which are supported on .NET 8 and later versions.

What are the Limitations of AOT Deployment?

Native AOT deployment in .Net has certain limitations, including:

Specific Runtime Environment:

Native AOT applications are targeted to a particular runtime environment, such as Linux x64 or Windows x64, similar to publishing a self-contained app. This means that the generated native code is optimized for a specific platform and may not be compatible with or runnable on other platforms.

Limited Library Compatibility: In .NET 7, only a limited number of libraries are fully compatible with native AOT compilation. While efforts are being made to expand library compatibility, it’s important to note that not all libraries can currently be used in conjunction with the Native AOT deployment model.

What are the Benefits of Native AOT deployment?

In .NET 7, the native AOT deployment enables the creation of self-contained, platform-specific executables without the need for runtime JIT. Native AOT apps exhibit fast startup times and optimized memory usage. These applications can be deployed on machines or containers without requiring a separate .NET runtime installation.

Looking ahead to .NET 8, Microsoft plans to extend native AOT support to ASP.NET Core, initially centering on cloud-focused API applications built with Minimal APIs. This enhancement aims to meet expectations regarding file size, startup time, memory usage, working set, and overall throughput performance.

Publishing native AOT

  1. Begin by creating two projects within a dotnet core 7 console application solution.
Sample Project — NativeAOTTest app (with AOT configuration)and Test app (without AOT configuration)
Sample project — NativeAOTTest app (with AOT configuration) and Test app (without AOT configuration)

2. Both the NativeAOTTest and Test applications have the same code in Program.cs. You can find the code on the GitHub repository.

3. To publish using AOT, add the statement “PublishAOT = true” to your project file (e.g., NativeAOT.csproj). This step is crucial for publishing the application using AOT.

Project file configuration for Native AOT deployment

4. Right-click on your project in the solution and select “Publish”.

5. Click the “Add Publish profile” button and choose “Folder” as the target. You can also select “Folder” as the specific target.

6. Click “Next” and proceed to the next page. Select the publish location (e.g., bin\Release\net7.0\publish). Click “Finish” to create the publish profile.

7. Adjust the profile settings as follows:

a. Deployment mode = Self-contained

b. Target runtime = win-x64

c. File publish options: Produce single file = checked

Profile settings for AOT deployment

8. You can continue the publishing process using the Developer Terminal. Right-click on your application in the solution and choose “Open in Terminal.”

9. In the Developer PowerShell window, run the command “dotnet publish -r win-x64 -c Release”. The message “Generating native code” will appear, indicating the use of AOT for deployment.

Native AOT deployment by Developer PowerShell

10. Navigate to the publish folder and observe the generated .exe file. Its size should be only 2.938 KB. Additionally, the execution of the code should be similar to an application deployed without AOT.

NativeAOT.exe result and published files

Publishing Without native AOT

  1. Adjust the profile settings for the Test application as follows. These settings should be the same as the NativeAOTTest application. Please note that the only difference lies in the csproj configuration up to this point.

a. Deployment mode = Self-contained

b. Target runtime = win-x64

c. File publish options: Produce single file = checked

Publish profile setting of the application

2. Save the profile settings and click the “Publish” button. The publishing process may take approximately 31 seconds, which is longer compared to AOT deployment. NativeAOTTest deployment typically takes around 16 seconds.

Publish without native AOT

3. The size of the Test project is 65.307 KB, which is significantly larger compared to deploying with native AOT. The execution time of the application appears to be similar to that of the other project.

Test.exe result and published files

Notes

I recommend watching the ‘Deep dive into .NET performance and native AOT’ session from the Microsoft Build conference. This session provides valuable insights into the deployment of mini services using Native AOT and showcases its efficiency in action.

Conclusion

Native AOT deployment offers significant opportunities for improving startup times and optimizing memory usage. This deployment model is enabled through self-contained publications, providing enhanced performance for Minimal APIs in .NET 8, and promising even better performance in the future. However, it does have certain limitations, including the requirement of installing specific workloads and libraries.

As with any application, choosing the deployment model according to the needs will give the best results at the end of the day.

References

1. https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?tabs=net7

2. https://learn.microsoft.com/en-us/dotnet/core/deploying/

3. https://github.com/dotnet/aspnetcore/issues/45910

--

--