.NET 8 | What is Native AOT in .NET?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right you heard about aot in net 8 that's great but what is aot and how can we use it in this video you'll learn everything you need to know about it we're not just scratching the surface here let's find out how it works when to use it and when you might not want to use it you will also find out that aot has been there with Net 7 already however you might not have heard about it and that has its reason before we open Visual Studio to code an application using aot let's quickly think about and learn about what aot actually is aot stands for ahead of time and is the counterpart to chit or just in time compilation with ahead of time compilation we generate the native code that runs on the machine right with the compilation of the application whereas with the traditional cheat compiler we turn our C code into IL code that will be then transformed into native code using the cheat compiler on the target machine for example a winforms or WPF application is compiled to I code on my developer PC and executed on another computer which uses the chit to compile the El code into Windows x64 native machine code now let's see how ahead of time compilation changes the traditional net application architecture we also have the C application code and the net SDK additionally we have the aot compiler in the case of compiling on Windows we need to install the C++ desktop workloud to get the C++ compiler when we compile and run the app locally we do not use ahead of time compilation we can run and debug the application like all nonn aot applications however when we run the net publish command the compilation will produce native code instead of Il code when executing the application we still need a net run time the application is still managed has a garbage collector and works like any other net application the only thing missing is the justing time or jit compiler the executable already contains native code and the operating system can direct ly lunch it like a native C++ application the output is a single executable file and its file size has been reduced because we trim all nonrequired code the executable is also platform dependent and non-portable with Net 7 we could already use ahead of time compilation however it was limited to console applications and therefore we haven't heard a lot about it with net 8 we can now use ahead of time compilation or aot to implement asp.net core-based apis for cloud native Solutions a new asp.net core web API native aot project template is available in Visual Studio 2022 17.8 there is also a separate project template for grpc using native aot again on Windows we need to make sure we have the desktop development with C++ workload installed otherwise the net publish command will fail let's create a project and take a look at it the generated project is preconfigured for Native aot and comes with a single program. CS file it uses the create slim Builder method instead of the create Builder which we normally use and it uses minimal apis instead of controllers let's quickly look at the different application Builders the create Builder we often use fully configures the application with Services we use such as configuring it to be able to run on IAS we don't get that with the slim Builder also the routing system is still there but we cannot use regx routes those changes allow for smaller executables we'll look at the other parts of the code later now let's quickly look at the project file we see the publish aot property which tells the compiler to use ahead of time compilation back in the program the Cs file we see that we have a to-do's route that returns a collection of five to-do items let's launch the application and test it in the browser we get the/ toos route and the example data is returned as expected now let's open the file explorer and navigate to to the project in the bin debug folder we see that we have a DL and an executable file both those files are very small the reason is that we don't use aot when we run the application from within visual studio also as we see from its size the application is not self-contained meaning it needs a. net runtime installed on the computer running the program it works for a developer machine and is the fastest way to start the application when working on it now let's publish the application using the console to see how aot works we can see that the step of generating native code takes much longer than when we just compile it and run the application let's head back to the Windows Explorer in the bin folder we now also have a release folder besides the debug folder with net 8 the default for publishing is released instead of debug in the release folder we have a net 8.0 folder and within that folder we see the win xx4 folder within that folder we have all the DLS that we need to run the application in the publish folder we see four files we have two application config files we need them when we want to load configuration from files if you load the configuration from environment variables we don't need them next we have the executable and the pdb file the pdb file is for debugging all we need to run the application is the executable and as you can see the executable only takes about 9 megabytes in this case let's test it I launched the application using a Windows console as you can see we get the lock output that the application is running in production mode Let's try to access the a API using a browser as we can see we get the data as expected what do we learn from this simple example when we launch the application within Visual Studio it gets compiled into IL code and we still use it exactly the same as any traditional net application however when we use the net publish command the code gets transformed into native code and we get a single executable file there is one more thing I want to show you in the program that CS file besides the record definition for the to-do type we also have an app Jason serializer context with a Json serializer attribute the context is registered with the configure HTP chasing options it's the code that makes the data Ser realizable without using reflection and therefore executable without a chit compiler great we Now understand what aot is and how it works but what advantages does it have over traditional net applications without aot of course we get better startup performance because the application does not go through the justing time compilation at startop the transformation step from IL to native code does not happen because it was already completed when the application was compiled it's especially helpful when building Cloud native applications that scale you can save a lot of time when frequently starting new instances of your applications another Advantage is the smaller app size with aot enabled we get rid of code that we don't need aot applications compiled to a single executable meaning the net runtime is part of the executable of course we need less code when we do not include the just in time compiler code also with trimming and other techniques we get the file size down by almost 10x it is helpful when deploying the application last but not least we use less memory using aot that saves US money when running in the cloud or allows us to run more instances in parallel using the same Hardware resources that's great and all but what are the disadvantages of using aot no jit compiler means that the native code has to be fully generated at compile time there is no runtime code generation no assembly loading no reflection and no expression compilation for example we need extra steps to serialize Jason data let's quickly talk about using reflection reflection works on I code because the runtime can explore the classes and properties when you run native code we cannot do that anymore there aren't classes in native code it means that we need to find find another solution to serialized data using aot than when you're using reflection compiling to native code using aot takes extra time compared to compiling to ilil also we require platform specific tools for example I use Windows and need the visual studio C++ tools when you run Linux you need cang and xcode is required for Mech users therefore you cannot publish cross plat form using aot when you compile on Windows your application is native Windows x64 code when you compile on Linux your executable won't run on Windows compared to traditional net applications aot applications aren't portable using modern tools such as the windows subsystem for Linux virtual machines or Docker containers we can get around it but still we need to consider it last but not not least libraries need to be aot compatible if you use a library that uses reflection you will run into warnings and errors when trying to publish the application using aot when should we use aot when building Cloud native apis aot can help us increase the performance and decrease the resources we need to run the application using the traditional net architecture is right when we don't need at extra performance and want to keep our applications portable also for some types of donet applications aot is not available with net 8 yet with net 8 the focus is on supporting Cloud native apis the following things are supported castra HTTP server middleware minimal apis including the request delegate generator grpc JWT Authentication authorization ado.net posc SQL SQL light deer. aot and nanon Norm not supported yet but a goal for future net versions are asp.net core MVC web API Razer Pages Blazer and Signal R and Entity framework core it said that Entity framework core is a target for net 9 you can build apis with asp.net core using aot but not use the whole of asp.net core web API is supported I strongly believe that Microsoft will do a lot and improve the support for aot with net 9 maybe we even get it for bler applications we learned what aot is how to create a net application that uses aot and what its advantages and disadvantages are I think it's a fascinating technology that allows the net applications to be very performant in Cloud native scenarios where it competes with other Technologies with its current state aot in. net 8 is limited to building Cloud native apis however I assume we will see broader support in the future and now what do you think of aot in net8 let me know in the comments below and if you want to see more videos like this consider subscribing to the channel and I will see you in the next video by
Info
Channel: Claudio Bernasconi
Views: 7,726
Rating: undefined out of 5
Keywords: Native AOT, Native AOT C#, Native AOT .NET, Native AOT .NET 8, .NET 8, ASP.NET Core Native AOT, Native AOT APIs, .NET APIs using AOT, Ahead of time compilation, Just in time compilation, AOT .NET, Claudio Bernasconi, dotnet 8, dotnet
Id: f-jRO957g98
Channel Id: undefined
Length: 13min 33sec (813 seconds)
Published: Fri Nov 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.