UseIISIntegration - ทำงานร่วมกับ IIS ใน ASP.NET Core
UseIISIntegration() เป็นเมธอดสำหรับตั้งค่า ASP.NET Core ให้ทำงานร่วมกับ IIS (Internet Information Services) ในโหมด Reverse Proxy
สถาปัตยกรรม: IIS + Kestrel (Out-of-Process)
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Client │────▶│ IIS │────▶│ Kestrel │
│ (Browser) │ │ (Reverse │ │ (ASP.NET │
│ │ │ Proxy) │ │ Core App) │
└─────────────┘ └─────────────┘ └─────────────┘
│ │
- Static Files - Business Logic
- SSL Termination - Controllers
- Windows Auth - Middleware
- Process Management - DI Container
UseIISIntegration ทำอะไร?
- ตั้งค่า Kestrel ให้ Listen ที่ Dynamic Port** (มักเป็น port สูงสุ่ม) ไม่ใช่ 80/443
- ตั้งค่า Forwarded Headers** (X-Forwarded-For, X-Forwarded-Proto) ให้ Kestrel รู้ IP/Protocol จริงของ Client
- ตั้งค่า Windows Authentication ผ่าน IIS
- จัดการ Process Lifetime** - IIS จัดการ Process (w3wp.exe) เริ่ม/หยุด/Restart
โฮสติ้งโมเดล: In-Process vs Out-of-Process
| คุณสมบัติ | In-Process (ASP.NET Core 2.2+ Default) | Out-of-Process (Legacy) |
|---|---|---|
| Process | w3wp.exe (IIS Worker Process) | dotnet.exe (แยกจาก IIS) |
| Kestrel | ไม่ได้ใช้ (IIS เป็น Server โดยตรง) | ใช้ Kestrel เป็น Internal Server |
| Performance | ดียกว่า (ไม่มี Network Hop) | มี Overhead จาก Proxy |
| UseIISIntegration | ไม่จำเป็น (แต่ยังใช้ได้) | จำเป็น |
| web.config | aspNetCore hostingModel="InProcess" | hostingModel="OutOfProcess" |
การตั้งค่าใน Program.cs (.NET 6+)
In-Process (Default - ไม่ต้องเรียก UseIISIntegration)
var builder = WebApplication.CreateBuilder(args);
// ไม่ต้องเรียก UseIISIntegration() - ใช้ IIS HTTP Server โดยตรง
var app = builder.Build();
app.Run();
Out-of-Process (หากต้องการบังคับ)
var builder = WebApplication.CreateBuilder(args);
// บังคับ Out-of-Process
builder.WebHost.UseIISIntegration(); // ใช้ Kestrel + IIS Proxy
var app = builder.Build();
app.Run();
การตั้งค่าใน web.config (สร้างอัตโนมัติ)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*"
modules="AspNetCoreModuleV2"
resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet"
arguments=".\MyApp.dll"
stdoutLogEnabled="false"
stdoutLogFile=".\logs\stdout"
hostingModel="InProcess" /> <!-- หรือ OutOfProcess -->
</system.webServer>
</location>
</configuration>
ASP.NET Core Module (ANCM)
- ANCM V2 (AspNetCoreModuleV2) - รองรับทั้ง In-Process และ Out-of-Process
- ติดตั้งมาพร้อม .NET Core Hosting Bundle บน Server IIS
- จัดการ Process Lifetime, Logging, Environment Variables
Forwarded Headers - สำคัญเมื่ออยู่หลัง Proxy
เมื่ออยู่หลัง IIS (หรือ Load Balancer/Reverse Proxy ใดๆ) ต้องตั้งค่า Forwarded Headers:
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto |
ForwardedHeaders.XForwardedHost;
// Trust ทุก Proxy (ระวัง Security - ควรระบุ KnownNetworks/KnownProxies)
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
var app = builder.Build();
// ใช้ Forwarded Headers ก่อน middleware อื่นๆ
app.UseForwardedHeaders();
// ... middleware อื่นๆ
เมื่อไหร่ควรใช้ UseIISIntegration?
| สถานการณ์ | แนะนำ |
|---|---|
| IIS บน Windows Server (Production) | In-Process (Default) - ไม่ต้องเรียก |
| ต้องการ Windows Authentication | In-Process รองรับได้ดี |
| Legacy App / Compatibility Issue | Out-of-Process + UseIISIntegration() |
| Linux Container (Docker/K8s) | ไม่ใช้ IIS - ใช้ Kestrel โดยตรง |
| Azure App Service (Windows) | In-Process (Default) |
สรุป
- UseIISIntegration() = ตั้งค่า Kestrel ให้ทำงานร่วม IIS (Out-of-Process)
- ASP.NET Core 2.2+ Default = In-Process - ไม่ต้องเรียก UseIISIntegration
- In-Process เร็วกว่า** ใช้ IIS HTTP Server โดยตรงใน w3wp.exe
- Out-of-Process** ใช้ Kestrel + IIS Reverse Proxy (ต้อง UseIISIntegration)
- ForwardedHeaders** จำเป็นเมื่ออยู่หลัง Proxy/Load Balancer