Nguon: Microsoft Learn · .NET 8.0

Hướng dẫn: Publish ứng dụng ASP.NET Core sử dụng Native AOT

Nguồn: Tutorial: Publish an ASP.NET Core app using Native AOT

.NET native ahead-of-time (AOT) có sẵn trong ASP.NET Core.

Minimal APIs không tương thích với native AOT.

Xem Native AOT deployment để biết thêm thông tin, bao gồm:

Điều kiện tiên quyết (Prerequisites)

.NET CLI

Visual Studio 2022 là bắt buộc vì Native AOT yêu cầu link.exe và các thư viện static runtime Visual C++. Không có kế hoạch hỗ trợ Native AOT mà không có Visual Studio.

Visual Studio

.NET SDK


Tạo web app với Native AOT

Tạo một ứng dụng ASP.NET Core API được cấu hình để hoạt động với Native AOT:

.NET CLI

Chạy các lệnh sau:

dotnetcli
dotnet new webapiaot -o MyFirstAotWebApi && cd MyFirstAotWebApi

Output (đầu ra) tương tự như ví dụ sau được hiển thị:

output
The template "ASP.NET Core Web API (Native AOT)" was created successfully.

Processing post-creation actions...
Restoring C:\Code\Demos\MyFirstAotWebApi\MyFirstAotWebApi.csproj:
  Determining projects to restore...
  Restored C:\Code\Demos\MyFirstAotWebApi\MyFirstAotWebApi.csproj (in 302 ms).
Restore succeeded.

Visual Studio

  1. Tạo một project mới ASP.NET Core Web API (Native AOT).
  2. Đặt tên project là MyFirstAotWebApi.
  3. Chọn Create (Tạo).

Publish ứng dụng Native AOT

Xác minh ứng dụng có thể được publish bằng Native AOT:

.NET CLI

dotnetcli
dotnet publish

Visual Studio

Visual Studio không hỗ trợ publish ứng dụng AOT. Sử dụng lệnh CLI:

dotnetcli
dotnet publish

Lệnh dotnet publish:

Output tương tự như ví dụ sau được hiển thị:

output
MSBuild version 17.<version> for .NET
  Determining projects to restore...
  Restored C:\Code\Demos\MyFirstAotWebApi\MyFirstAotWebApi.csproj (in 241 ms).
C:\Code\dotnet\aspnetcore\.dotnet\sdk\8.0.<version>\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(287,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [C:\Code\Demos\MyFirstAotWebApi\MyFirstAotWebApi.csproj]
  MyFirstAotWebApi -> C:\Code\Demos\MyFirstAotWebApi\bin\Release\net8.0\win-x64\MyFirstAotWebApi.dll
  Generating native code
  MyFirstAotWebApi -> C:\Code\Demos\MyFirstAotWebApi\bin\Release\net8.0\win-x64\publish\

Output có thể khác với ví dụ trước tùy thuộc vào phiên bản .NET 8 được sử dụng, thư mục được dùng và các yếu tố khác.

Xem xét nội dung của thư mục output:

code
dir bin\Release\net8.0\win-x64\publish

Output tương tự như ví dụ sau được hiển thị:

Output
    Directory: C:\Code\Demos\MyFirstAotWebApi\bin\Release\net8.0\win-x64\publish

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          30/03/2023  1:41 PM        9480704 MyFirstAotWebApi.exe
-a---          30/03/2023  1:41 PM       43044864 MyFirstAotWebApi.pdb

File thực thi (executable) là self-contained (tự chứa) và không yêu cầu .NET runtime để chạy. Khi khởi chạy, nó hoạt động giống như ứng dụng chạy trong môi trường Development. Chạy ứng dụng AOT:

code
.\bin\Release\net8.0\win-x64\publish\MyFirstAotWebApi.exe

Output tương tự như ví dụ sau được hiển thị:

output
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Code\Demos\MyFirstAotWebApi

Sử dụng thư viện với Native AOT

Nhiều thư viện phổ biến được dùng trong các project ASP.NET Core hiện có một số vấn đề tương thích khi được tích hợp vào các project nhắm mục tiêu Native AOT, chẳng hạn như:

Các thư viện sử dụng các tính năng động này cần được cập nhật để hoạt động với Native AOT. Nhiều công cụ có sẵn để áp dụng các cập nhật cần thiết, chẳng hạn như Roslyn source generators.

Các tác giả thư viện muốn hỗ trợ Native AOT được khuyến khích xem xét các bài viết sau: