Nguon: Microsoft Learn · .NET 8.0

In-process hosting (Hosting trong tiến trình) với IIS và ASP.NET Core

Nguồn: In-process hosting with IIS and ASP.NET Core

In-process hosting chạy ứng dụng ASP.NET Core trong cùng process với IIS worker process. In-process hosting cung cấp hiệu suất được cải thiện so với out-of-process hosting vì các request không được proxy qua loopback adapter (bộ điều hợp vòng lặp), một network interface trả lại lưu lượng mạng đi ra về cùng máy.

Sơ đồ sau minh họa mối quan hệ giữa IIS, ASP.NET Core Module và ứng dụng được host in-process:

!ASP.NET Core Module trong kịch bản hosting in-process

Bật in-process hosting

Kể từ ASP.NET Core 3.0, in-process hosting được bật theo mặc định cho tất cả ứng dụng được triển khai lên IIS.

Để cấu hình tường minh ứng dụng cho in-process hosting, đặt giá trị của thuộc tính <AspNetCoreHostingModel> thành InProcess trong project file (.csproj):

xml
<PropertyGroup>
  <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

Kiến trúc tổng quan

Luồng chung của một request như sau:

  1. Request đến từ web đến kernel-mode HTTP.sys driver.
  2. Driver định tuyến native request đến IIS trên port được cấu hình của website, thường là 80 (HTTP) hoặc 443 (HTTPS).
  3. ASP.NET Core Module nhận native request và chuyển nó đến IIS HTTP Server (IISHttpServer). IIS HTTP Server là một in-process server implementation cho IIS, chuyển đổi request từ native sang managed.

Sau khi IIS HTTP Server xử lý request:

  1. Request được gửi đến ASP.NET Core middleware pipeline (đường ống phần mềm trung gian).
  2. Middleware pipeline xử lý request và chuyển nó như một instance HttpContext đến logic của ứng dụng.
  3. Phản hồi của ứng dụng được chuyển lại IIS thông qua IIS HTTP Server.
  4. IIS gửi phản hồi đến client đã khởi tạo request.

CreateDefaultBuilder thêm một instance IServer bằng cách gọi phương thức UseIIS để boot CoreCLR và host ứng dụng bên trong IIS worker process (w3wp.exe hoặc iisexpress.exe). Performance tests (kiểm tra hiệu suất) cho thấy rằng hosting ứng dụng .NET in-process mang lại throughput request cao hơn đáng kể so với hosting ứng dụng out-of-process và proxy request đến Kestrel.

Các ứng dụng được publish dưới dạng single file executable không thể được tải bởi mô hình hosting in-process.

Cấu hình ứng dụng

Để cấu hình tùy chọn IIS, bao gồm cấu hình dịch vụ cho IISServerOptions trong Program.cs. Ví dụ sau vô hiệu hóa AutomaticAuthentication:

csharp
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Server.IIS;
using Microsoft.EntityFrameworkCore;
using RPauth.Data;

var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();

builder.Services.Configure<IISServerOptions>(options =>
{
    options.AutomaticAuthentication = false;
});

builder.Services.AddTransient<IClaimsTransformation, MyClaimsTransformation>();
builder.Services.AddAuthentication(IISServerDefaults.AuthenticationScheme);

builder.Services.AddRazorPages();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();

app.Run();
Tùy chọnMặc địnhMô tả
AutomaticAuthenticationtrueNếu true, IIS Server đặt HttpContext.User được xác thực bởi Windows Authentication. Nếu false, server chỉ cung cấp danh tính cho HttpContext.User và phản hồi thách thức khi được yêu cầu tường minh bởi AuthenticationScheme. Windows Authentication phải được bật trong IIS để AutomaticAuthentication hoạt động.
AuthenticationDisplayNamenullĐặt tên hiển thị cho người dùng trên trang đăng nhập.
AllowSynchronousIOfalseLiệu I/O đồng bộ có được phép cho HttpContext.RequestHttpContext.Response.
MaxRequestBodySize30000000Lấy hoặc đặt kích thước phần thân request tối đa cho HttpRequest. Lưu ý rằng IIS tự thân có giới hạn maxAllowedContentLength sẽ được xử lý trước MaxRequestBodySize được đặt trong IISServerOptions. Thay đổi MaxRequestBodySize sẽ không ảnh hưởng đến maxAllowedContentLength.

Sự khác biệt giữa in-process và out-of-process hosting

Các đặc điểm sau áp dụng khi hosting in-process:

Lấy thông tin timing

Xem Lấy thông tin timing chi tiết với IHttpSysRequestTimingFeature.