I'm a bit confused about the .Net AOT (Ahead-Of-Time) compilation capabilities in .Net 7.0 and .Net 8.0.
I understand that it's possible to publish and deploy a whole application as a Native AOT application, if it meets certain criteria (https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?tabs=net7%2Cwindows). The limitations described speak in terms of the whole application (i.e., "Native AOT apps have the following limitations: No C++\CLI...." etc.), while the property is defined in a project file.
My question is, is it possible to define only a single project in your solution to be compiled to machine code using AOT (Ahead-Of-Time) compilation, or only some of them, while leaving the rest of the application projects to use the JIT compiler?
I've tried setting the property as true for a single infrastructure project in my solution and then publish my application, but I think it had no effect on the resulting files.
You can publish a DLL project with AOT and have other non-AOT projects in the same solution, but there are some heavy restrictions. Mainly:
Then, of course, put this in the AOT project file:
<PropertyGroup>
<PublishAot>true</PublishAot>
</PropertyGroup>
And enable AOT warnings:
<PropertyGroup>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>
...and publish like this (run this in the project directory, or as a post build event of another project than the AOT project, since it would recurse forever if it were in the AOT project):
dotnet publish -r win-x64
Your C# projects must include the DLL in their output directory (for example, include the DLL file in the project but mark it as 'Content' that should be copied to the output folder) and then call those AOT methods just as they would call a C++ function in a C++ DLL, like this, for example:
[DllImport("MyAotDll.dll")]
public static extern int MyMethodName();
...
MyMethodName();
So, publishing a C# DLL as a AOT DLL is as if you converted it to C++ and then built the C++ DLL. In fact, one advantage of an AOT DLL is that any unmanaged language can call its functions.