In an IIS-hosted ASP.NET Core 10 project, what does AspNetCoreHostingModel=OutOfProcess change in web.config and in the server that handles requests?
answer
- an MSBuild property, default inprocess
- written as hostingModel in web.config
- IISHttpServer vs Kestrel on 127.0.0.1
- ASPNETCORE_PORT and the pairing token
- IISServerOptions vs IISOptions
basics
~10 sPublish writes hostingModel="outofprocess" instead of the default inprocess. ANCM then starts the app as its own process and proxies to Kestrel on http://127.0.0.1:<port>, so IISOptions and Kestrel limits apply instead of IISHttpServer and IISServerOptions.
solid answer
~40 s`AspNetCoreHostingModel` is an SDK property; for .NET Core 3.0+ it defaults to `inprocess`, and publish copies it into `web.config` as `hostingModel`. In-process, ANCM loads the runtime into the IIS worker process and `UseIIS()` registers **`IISHttpServer`** as the server, configured through `IISServerOptions`. With `OutOfProcess`, ANCM launches `processPath` as a separate process and sets `ASPNETCORE_PORT`, `ASPNETCORE_APPL_PATH` and `ASPNETCORE_TOKEN`. `UseIISIntegration()` sees them, binds **Kestrel** to `http://127.0.0.1:<port>` with `PreferHostingUrls(true)` (so your Kestrel endpoints are ignored), rejects requests lacking the `MS-ASPNETCORE-TOKEN` header with 400, and forwards `X-Forwarded-For` and `X-Forwarded-Proto`. Settings then come from `IISOptions` and `KestrelServerLimits`. `WebApplication.CreateBuilder` calls both `UseIIS` and `UseIISIntegration`, so no code change is needed.
code
xml · 7 lines<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<!-- Default is inprocess; publish writes this into web.config as hostingModel -->
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>
</Project>go deeper
Recall that AspNetCoreHostingModel defaults to InProcess and that publish writes it into web.config as hostingModel.
Explain IISHttpServer versus Kestrel on 127.0.0.1, the ASPNETCORE_PORT and token handshake, and which options type applies to each model.
Diagnose model-specific surprises: ignored Kestrel endpoints, IISServerOptions with no effect, a second in-process app in one pool, and CreateSlimBuilder lacking IIS integration.
Decide a hosting-model standard for a Windows estate, balancing an extra network hop against process isolation and app-pool sharing.
## The property and where it ends up `AspNetCoreHostingModel` is an MSBuild property read by `Microsoft.NET.Sdk.Web`. When it is empty and the project targets .NET Core 3.0 or later, the SDK sets it to `inprocess`. At publish time the web.config transform copies it into the `aspNetCore` element: ```xml <PropertyGroup> <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel> </PropertyGroup> ``` produces `hostingModel="OutOfProcess"`. The value is case-insensitive; anything other than `InProcess` or `OutOfProcess` fails publish. Two details catch people: - If the project contains its own `web.config` that **already sets `hostingModel`**, publish keeps that value and ignores the property. - ANCM itself treats a **missing** `hostingModel` attribute as out-of-process. The in-process default exists only because the SDK writes the attribute. ## What runs where | Aspect | `inprocess` (SDK default) | `outofprocess` | |---|---|---| | Process | the IIS worker process | a separate app process started by ANCM | | Server inside ASP.NET Core | `IISHttpServer`, registered by `UseIIS()` | Kestrel, bound by `UseIISIntegration()` | | Options type | `IISServerOptions` | `IISOptions` plus `KestrelServerLimits` | | Binding | none of your own; IIS owns the bindings | `http://127.0.0.1:<ASPNETCORE_PORT>`, `PreferHostingUrls` forced on | | Apps per app pool | one in-process app per pool | several apps may share a pool | The last row is enforced by ANCM: a second in-process app in the same pool fails with "Only one in-process application is allowed per IIS application pool." ## The in-process path `WebApplication.CreateBuilder` calls `UseIIS()`. When the process was started by IIS with ANCM loaded, `UseIIS()`: 1. replaces the `IServer` registration with `IISHttpServer`, which receives requests directly from the IIS pipeline without a network hop; 2. sets the content root to the IIS application's physical path; 3. configures `IISServerOptions`, including `MaxRequestBodySize` copied from IIS's `maxAllowedContentLength` (30,000,000 bytes unless request filtering says otherwise); 4. registers Windows authentication support when the site has Windows or Basic authentication enabled (`AutomaticAuthentication` defaults to `true`). Outside IIS the same call does nothing, so `dotnet run` still starts Kestrel. ## The out-of-process path ANCM starts the app as a child process and sets three environment variables: `ASPNETCORE_PORT` (a port it chose), `ASPNETCORE_APPL_PATH` (the IIS application's virtual path) and `ASPNETCORE_TOKEN` (a pairing token). `UseIISIntegration()` reacts only when all three are present: - It sets `urls` to `http://127.0.0.1:<port>` and forces **`PreferHostingUrls(true)`**, so `Kestrel:Endpoints` and `ConfigureKestrel` listen calls are overridden. - It adds a startup filter whose middleware returns **400** for any request without the matching `MS-ASPNETCORE-TOKEN` header, so only ANCM can talk to the app on that port. - It sets the path base from `ASPNETCORE_APPL_PATH` for apps under a virtual directory. - It configures forwarded headers for `X-Forwarded-For` and `X-Forwarded-Proto`, so `RemoteIpAddress` and `Request.Scheme` reflect the original client. - ANCM can ask the app to shut down by posting to `/iisintegration` with an `MS-ASPNETCORE-EVENT: shutdown` header. Because Kestrel is the server here, Kestrel's limits apply on top of whatever IIS enforces, and `IISOptions` (`AutomaticAuthentication`, `AuthenticationDisplayName`, `ForwardClientCertificate`) configures the Windows-authentication and client-certificate forwarding. ## Code you do not need to change Because `CreateBuilder` registers both `UseIIS()` and `UseIISIntegration()`, switching models is a project-file change. Code that assumes one model is the risk: an app that reads `IISServerOptions` in an out-of-process deployment configures nothing, and an app that configures Kestrel endpoints for IIS finds them ignored. `WebApplication.CreateSlimBuilder` registers neither, so an IIS-hosted app must not use it unless it adds the IIS integration itself. ## Mistakes seen when switching models 1. Setting `AspNetCoreHostingModel` while a hand-written `web.config` in the project already pins `hostingModel`; publish keeps the file's value, so nothing changes. 2. Moving to out-of-process and expecting `IISServerOptions.MaxRequestBodySize` to keep working; Kestrel's limits now apply. 3. Adding `ConfigureKestrel` listen calls for an out-of-process site; `PreferHostingUrls` overrides them. 4. Reading the client IP without the forwarded headers that `UseIISIntegration()` configures, then wondering why every request seems to come from `127.0.0.1`. 5. Publishing two in-process apps into one application pool, which ANCM refuses at the first request. ## Telling which model is running - In the process list, an in-process app runs inside the app pool's worker process; an out-of-process app is its own `.exe` or `dotnet` process. - `HttpContext.Features.Get<IServerVariablesFeature>()` is available in-process, where IIS server variables can be read. - The startup log of an out-of-process app shows Kestrel listening on `http://127.0.0.1:<port>`.
- Why does Kestrel:Endpoints in appsettings.json have no effect under out-of-process IIS hosting?`UseIISIntegration()` sets the `urls` value to `http://127.0.0.1:<ASPNETCORE_PORT>` and calls `PreferHostingUrls(true)`. With that switch on, Kestrel binds the address list and overrides any configured endpoints, because ANCM must know exactly where to forward requests.
- What protects an out-of-process app's local port from other processes on the server?Kestrel listens on loopback only, and the IIS integration middleware rejects every request that lacks the `MS-ASPNETCORE-TOKEN` header carrying the pairing token ANCM passed in `ASPNETCORE_TOKEN`, answering 400.
- Can two ASP.NET Core apps share one IIS application pool?Not in-process: ANCM refuses the second one with "Only one in-process application is allowed per IIS application pool." Out-of-process apps run in their own processes and can share a pool.
- Out-of-process is the default hosting model for new projects.
- IISServerOptions configures an out-of-process app's limits.
- Switching hosting model requires rewriting Program.cs.
- Kestrel:Endpoints still decide the port when hosted out-of-process.
- Several in-process apps can share one IIS application pool.