|
| 1 | +:::moniker range="= aspnetcore-8.0" |
| 2 | + |
| 3 | +[!INCLUDE[](~/includes/not-latest-version.md)] |
| 4 | + |
| 5 | +ASP.NET Core 8.0 introduces support for [.NET native ahead-of-time (AOT)](/dotnet/core/deploying/native-aot/). |
| 6 | + |
| 7 | +> [!NOTE] |
| 8 | +> * In .NET 8, not all ASP.NET Core features are compatible with Native AOT. |
| 9 | +> * Tabs are provided for the [.NET CLI](/dotnet/core/tools/) and [Visual Studio](https://visualstudio.microsoft.com/downloads/) instructions: |
| 10 | +> * Visual Studio is a prerequisite even if the CLI tab is selected. |
| 11 | +> * The CLI must be used to publish even if the Visual Studio tab is selected. |
| 12 | +
|
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +# [.NET CLI](#tab/net-cli) |
| 16 | + |
| 17 | +* [!INCLUDE[](~/includes/8.0-SDK.md)] |
| 18 | +* On Linux, see [Prerequisites for Native AOT deployment](/dotnet/core/deploying/native-aot/?tabs=net8plus#prerequisites-for-native-aot-deployment). |
| 19 | +* [Visual Studio 2022](https://visualstudio.microsoft.com/downloads/) with the **Desktop development with C++** workload installed. |
| 20 | + |
| 21 | +  |
| 22 | + |
| 23 | +> [!NOTE] |
| 24 | +> Visual Studio 2022 is required because Native AOT requires [link.exe](/cpp/build/reference/linker-options) and the Visual C++ static runtime libraries. There are no plans to support Native AOT ***without*** Visual Studio. |
| 25 | +
|
| 26 | +# [Visual Studio](#tab/visual-studio) |
| 27 | + |
| 28 | +* [!INCLUDE[](~/includes/8.0-SDK.md)] |
| 29 | + |
| 30 | +* [Visual Studio 2022](https://visualstudio.microsoft.com/downloads/) with the following workloads installed: |
| 31 | + * **ASP.NET and web development** |
| 32 | + * **Desktop development with C++** |
| 33 | + |
| 34 | +  |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Create a web app with Native AOT |
| 39 | + |
| 40 | +Create an ASP.NET Core API app that is configured to work with Native AOT: |
| 41 | + |
| 42 | +# [.NET CLI](#tab/net-cli) |
| 43 | + |
| 44 | +Run the following commands: |
| 45 | + |
| 46 | +```dotnetcli |
| 47 | +dotnet new webapiaot -o MyFirstAotWebApi && cd MyFirstAotWebApi |
| 48 | +``` |
| 49 | + |
| 50 | +Output similar to the following example is displayed: |
| 51 | + |
| 52 | +```output |
| 53 | +The template "ASP.NET Core Web API (Native AOT)" was created successfully. |
| 54 | +
|
| 55 | +Processing post-creation actions... |
| 56 | +Restoring C:\Code\Demos\MyFirstAotWebApi\MyFirstAotWebApi.csproj: |
| 57 | + Determining projects to restore... |
| 58 | + Restored C:\Code\Demos\MyFirstAotWebApi\MyFirstAotWebApi.csproj (in 302 ms). |
| 59 | +Restore succeeded. |
| 60 | +``` |
| 61 | + |
| 62 | +# [Visual Studio](#tab/visual-studio) |
| 63 | + |
| 64 | +1. Create a new `ASP.NET Core Web API (Native AOT)` project. |
| 65 | +1. Name the project **MyFirstAotWebApi**. |
| 66 | +1. Select **Create**. |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## Publish the Native AOT app |
| 71 | + |
| 72 | +Verify the app can be published using Native AOT: |
| 73 | + |
| 74 | +# [.NET CLI](#tab/net-cli) |
| 75 | + |
| 76 | +```dotnetcli |
| 77 | +dotnet publish |
| 78 | +``` |
| 79 | + |
| 80 | +# [Visual Studio](#tab/visual-studio) |
| 81 | + |
| 82 | +Visual studio doesn't support publishing an AOT app. Use the CLI command: |
| 83 | + |
| 84 | +```dotnetcli |
| 85 | +dotnet publish |
| 86 | +``` |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +The `dotnet publish` command: |
| 91 | + |
| 92 | +* Compiles the source files. |
| 93 | +* Generates source code files that are compiled. |
| 94 | +* Passes generated assemblies to a native IL compiler. The IL compiler produces the native executable. The native executable contains the native machine code. |
| 95 | + |
| 96 | +Output similar to the following example is displayed: |
| 97 | + |
| 98 | +```output |
| 99 | +MSBuild version 17.<version> for .NET |
| 100 | + Determining projects to restore... |
| 101 | + Restored C:\Code\Demos\MyFirstAotWebApi\MyFirstAotWebApi.csproj (in 241 ms). |
| 102 | +C:\Code\dotnet\aspnetcore\.dotnet\sdk\8.0.<version>\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIde |
| 103 | +ntifierInference.targets(287,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotne |
| 104 | +t-support-policy [C:\Code\Demos\MyFirstAotWebApi\MyFirstAotWebApi.csproj] |
| 105 | + MyFirstAotWebApi -> C:\Code\Demos\MyFirstAotWebApi\bin\Release\net8.0\win-x64\MyFirstAotWebApi.dll |
| 106 | + Generating native code |
| 107 | + MyFirstAotWebApi -> C:\Code\Demos\MyFirstAotWebApi\bin\Release\net8.0\win-x64\publish\ |
| 108 | +``` |
| 109 | + |
| 110 | +The output may differ from the preceding example depending on the version of .NET 8 used, directory used, and other factors. |
| 111 | + |
| 112 | +Review the contents of the output directory: |
| 113 | + |
| 114 | +``` |
| 115 | +dir bin\Release\net8.0\win-x64\publish |
| 116 | +``` |
| 117 | + |
| 118 | +Output similar to the following example is displayed: |
| 119 | + |
| 120 | +```Output |
| 121 | + Directory: C:\Code\Demos\MyFirstAotWebApi\bin\Release\net8.0\win-x64\publish |
| 122 | +
|
| 123 | +Mode LastWriteTime Length Name |
| 124 | +---- ------------- ------ ---- |
| 125 | +-a--- 30/03/2023 1:41 PM 9480704 MyFirstAotWebApi.exe |
| 126 | +-a--- 30/03/2023 1:41 PM 43044864 MyFirstAotWebApi.pdb |
| 127 | +``` |
| 128 | + |
| 129 | +The executable is self-contained and doesn't require a .NET runtime to run. When launched, it behaves the same as the app run in the development environment. Run the AOT app: |
| 130 | + |
| 131 | +``` |
| 132 | +.\bin\Release\net8.0\win-x64\publish\MyFirstAotWebApi.exe |
| 133 | +``` |
| 134 | + |
| 135 | +Output similar to the following example is displayed: |
| 136 | + |
| 137 | +```output |
| 138 | +info: Microsoft.Hosting.Lifetime[14] |
| 139 | + Now listening on: http://localhost:5000 |
| 140 | +info: Microsoft.Hosting.Lifetime[0] |
| 141 | + Application started. Press Ctrl+C to shut down. |
| 142 | +info: Microsoft.Hosting.Lifetime[0] |
| 143 | + Hosting environment: Production |
| 144 | +info: Microsoft.Hosting.Lifetime[0] |
| 145 | + Content root path: C:\Code\Demos\MyFirstAotWebApi |
| 146 | +``` |
| 147 | + |
| 148 | +[!INCLUDE[](~/fundamentals/aot/includes/aot_lib.md)] |
| 149 | + |
| 150 | +## See also |
| 151 | + |
| 152 | +* <xref:fundamentals/native-aot> |
| 153 | +* [Native AOT deployment](/dotnet/core/deploying/native-aot/) |
| 154 | +* [Using the configuration binder source generator](https://andrewlock.net/exploring-the-dotnet-8-preview-using-the-new-configuration-binder-source-generator/) |
| 155 | +* [The minimal API AOT compilation template](https://andrewlock.net/exploring-the-dotnet-8-preview-the-minimal-api-aot-template/) |
| 156 | +* [Comparing `WebApplication.CreateBuilder` to `CreateSlimBuilder`](https://andrewlock.net/exploring-the-dotnet-8-preview-comparing-createbuilder-to-the-new-createslimbuilder-method/) |
| 157 | +* [Exploring the new minimal API source generator](https://andrewlock.net/exploring-the-dotnet-8-preview-exploring-the-new-minimal-api-source-generator/) |
| 158 | +* [Replacing method calls with Interceptors](https://andrewlock.net/exploring-the-dotnet-8-preview-changing-method-calls-with-interceptors/) |
| 159 | +* [Configuration-binding source generator](/dotnet/core/whats-new/dotnet-8#configuration-binding-source-generator) |
| 160 | + |
| 161 | +:::moniker-end |
0 commit comments