Skip to content

Commit baf5463

Browse files
committed
AOT out of preview /2
1 parent 3cd06be commit baf5463

3 files changed

Lines changed: 171 additions & 8 deletions

File tree

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
> [!NOTE]
2-
> * The Native AOT feature is currently in preview.
3-
> * In .NET 8, not all ASP.NET Core features are compatible with Native AOT.
2+
> In .NET 8, not all ASP.NET Core features are compatible with Native AOT.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
![Visual Studio workload selection dialog showing "Desktop development with C++" selected.](~/fundamentals/aot/_static/cpponly.png)
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+
![Visual Studio workload selection dialog showing "ASP.NET and web development" and "Desktop development with C++" selected.](~/fundamentals/aot/_static/ddcpp.png)
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

aspnetcore/fundamentals/aot/native-aot-tutorial.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@ ms.topic: tutorial
77
content_well_notification: AI-contribution
88
ms.author: midenn
99
ms.custom: mvc
10-
ms.date: 08/10/2023
10+
ms.date: 5/10/2023
1111
uid: fundamentals/native-aot-tutorial
1212
ai-usage: ai-assisted
1313
---
1414
# Tutorial: Publish an ASP.NET Core app using Native AOT
1515

16+
:::moniker range="> aspnetcore-8.0"
17+
1618
<!-- UPDATE 9.0 Activate after release and INCLUDE is updated
1719
1820
[!INCLUDE[](~/includes/not-latest-version.md)]
1921
2022
-->
2123

22-
ASP.NET Core 8.0 introduces support for [.NET native ahead-of-time (AOT)](/dotnet/core/deploying/native-aot/).
24+
[.NET native ahead-of-time (AOT)](/dotnet/core/deploying/native-aot/) is available in ASP.NET Core.
2325

2426
> [!NOTE]
25-
> * The Native AOT feature is currently in preview.
26-
> * In .NET 8, not all ASP.NET Core features are compatible with Native AOT.
27+
> * Minimal APIs are ***not*** compatible with native AOT.
2728
> * Tabs are provided for the [.NET CLI](/dotnet/core/tools/) and [Visual Studio](https://visualstudio.microsoft.com/downloads/) instructions:
2829
> * Visual Studio is a prerequisite even if the CLI tab is selected.
2930
> * The CLI must be used to publish even if the Visual Studio tab is selected.
@@ -32,7 +33,7 @@ ASP.NET Core 8.0 introduces support for [.NET native ahead-of-time (AOT)](/dotne
3233

3334
# [.NET CLI](#tab/net-cli)
3435

35-
* [!INCLUDE[](~/includes/8.0-SDK.md)]
36+
* [!INCLUDE[](~/includes/9.0-SDK.md)]
3637
* On Linux, see [Prerequisites for Native AOT deployment](/dotnet/core/deploying/native-aot/?tabs=net8plus#prerequisites-for-native-aot-deployment).
3738
* [Visual Studio 2022](https://visualstudio.microsoft.com/downloads/) with the **Desktop development with C++** workload installed.
3839

@@ -43,7 +44,7 @@ ASP.NET Core 8.0 introduces support for [.NET native ahead-of-time (AOT)](/dotne
4344
4445
# [Visual Studio](#tab/visual-studio)
4546

46-
* [!INCLUDE[](~/includes/8.0-SDK.md)]
47+
* [!INCLUDE[](~/includes/9.0-SDK.md)]
4748

4849
* [Visual Studio 2022](https://visualstudio.microsoft.com/downloads/) with the following workloads installed:
4950
* **ASP.NET and web development**
@@ -175,3 +176,5 @@ info: Microsoft.Hosting.Lifetime[0]
175176
* [Exploring the new minimal API source generator](https://andrewlock.net/exploring-the-dotnet-8-preview-exploring-the-new-minimal-api-source-generator/)
176177
* [Replacing method calls with Interceptors](https://andrewlock.net/exploring-the-dotnet-8-preview-changing-method-calls-with-interceptors/)
177178
* [Configuration-binding source generator](/dotnet/core/whats-new/dotnet-8#configuration-binding-source-generator)
179+
180+
:::moniker-end

0 commit comments

Comments
 (0)