|
| 1 | +--- |
| 2 | +title: In-process hosting with IIS and ASP.NET Core |
| 3 | +author: rick-anderson |
| 4 | +description: Learn about in-Process hosting with IIS and the ASP.NET Core Module. |
| 5 | +monikerRange: '>= aspnetcore-5.0' |
| 6 | +ms.author: riande |
| 7 | +ms.custom: mvc |
| 8 | +ms.date: 02/07/2020 |
| 9 | +no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] |
| 10 | +uid: host-and-deploy/iis/in-process-hosting |
| 11 | +--- |
| 12 | + |
| 13 | +# In-process hosting with IIS and ASP.NET Core |
| 14 | + |
| 15 | +In-process hosting runs an ASP.NET Core app in the same process as its IIS worker process. In-process hosting provides improved performance over out-of-process hosting because requests aren't proxied over the loopback adapter, a network interface that returns outgoing network traffic back to the same machine. |
| 16 | + |
| 17 | +The following diagram illustrates the relationship between IIS, the ASP.NET Core Module, and an app hosted in-process: |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## Enable in-process hosting |
| 22 | + |
| 23 | +Since ASP.NET Core 3.0, in-process hosting has been enabled by default for all app deployed to IIS. |
| 24 | + |
| 25 | +To explicitly configure an app for in-process hosting, set the value of the `<AspNetCoreHostingModel>` property to `InProcess` in the project file (`.csproj`): |
| 26 | + |
| 27 | +```xml |
| 28 | +<PropertyGroup> |
| 29 | + <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> |
| 30 | +</PropertyGroup> |
| 31 | +``` |
| 32 | + |
| 33 | +## General architecture |
| 34 | + |
| 35 | +The general flow of a request is as follows: |
| 36 | + |
| 37 | +1. A request arrives from the web to the kernel-mode HTTP.sys driver. |
| 38 | +1. The driver routes the native request to IIS on the website's configured port, usually 80 (HTTP) or 443 (HTTPS). |
| 39 | +1. The ASP.NET Core Module receives the native request and passes it to IIS HTTP Server (`IISHttpServer`). IIS HTTP Server is an in-process server implementation for IIS that converts the request from native to managed. |
| 40 | + |
| 41 | +After the IIS HTTP Server processes the request: |
| 42 | + |
| 43 | +1. The request is sent to the ASP.NET Core middleware pipeline. |
| 44 | +1. The middleware pipeline handles the request and passes it on as an `HttpContext` instance to the app's logic. |
| 45 | +1. The app's response is passed back to IIS through IIS HTTP Server. |
| 46 | +1. IIS sends the response to the client that initiated the request. |
| 47 | + |
| 48 | +`CreateDefaultBuilder` adds an <xref:Microsoft.AspNetCore.Hosting.Server.IServer> instance by calling the <xref:Microsoft.AspNetCore.Hosting.WebHostBuilderIISExtensions.UseIIS%2A> method to boot the [CoreCLR](/dotnet/standard/glossary#coreclr) and host the app inside of the IIS worker process (`w3wp.exe` or `iisexpress.exe`). Performance tests indicate that hosting a .NET Core app in-process delivers significantly higher request throughput compared to hosting the app out-of-process and proxying requests to [Kestrel](xref:fundamentals/servers/kestrel). |
| 49 | + |
| 50 | +Apps published as a single file executable can't be loaded by the in-process hosting model. |
| 51 | + |
| 52 | +## Application configuration |
| 53 | + |
| 54 | +To configure IIS options, include a service configuration for <xref:Microsoft.AspNetCore.Builder.IISServerOptions> in <xref:Microsoft.AspNetCore.Hosting.IStartup.ConfigureServices%2A>. The following example disables AutomaticAuthentication: |
| 55 | + |
| 56 | +```csharp |
| 57 | +services.Configure<IISServerOptions>(options => |
| 58 | +{ |
| 59 | + options.AutomaticAuthentication = false; |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +| Option | Default | Setting | |
| 64 | +| ------ | :-----: | ------- | |
| 65 | +| `AutomaticAuthentication` | `true` | If `true`, IIS Server sets the `HttpContext.User` authenticated by [Windows Authentication](xref:security/authentication/windowsauth). If `false`, the server only provides an identity for `HttpContext.User` and responds to challenges when explicitly requested by the `AuthenticationScheme`. Windows Authentication must be enabled in IIS for `AutomaticAuthentication` to function. For more information, see [Windows Authentication](xref:security/authentication/windowsauth). | |
| 66 | +| `AuthenticationDisplayName` | `null` | Sets the display name shown to users on login pages. | |
| 67 | +| `AllowSynchronousIO` | `false` | Whether synchronous I/O is allowed for the `HttpContext.Request` and the `HttpContext.Response`. | |
| 68 | +| `MaxRequestBodySize` | `30000000` | Gets or sets the max request body size for the `HttpRequest`. Note that IIS itself has the limit `maxAllowedContentLength` which will be processed before the `MaxRequestBodySize` set in the `IISServerOptions`. Changing the `MaxRequestBodySize` won't affect the `maxAllowedContentLength`. To increase `maxAllowedContentLength`, add an entry in the `web.config` to set `maxAllowedContentLength` to a higher value. For more details, see [Configuration](/iis/configuration/system.webServer/security/requestFiltering/requestLimits/#configuration). | |
| 69 | + |
| 70 | +## Differences between in-process and out-of-process hosting |
| 71 | + |
| 72 | +The following characteristics apply when hosting in-process: |
| 73 | + |
| 74 | +* IIS HTTP Server (`IISHttpServer`) is used instead of [Kestrel](xref:fundamentals/servers/kestrel) server. For in-process, [`CreateDefaultBuilder`](xref:fundamentals/host/generic-host#default-builder-settings) calls <xref:Microsoft.AspNetCore.Hosting.WebHostBuilderIISExtensions.UseIIS%2A> to: |
| 75 | + |
| 76 | + * Register the `IISHttpServer`. |
| 77 | + * Configure the port and base path the server should listen on when running behind the ASP.NET Core Module. |
| 78 | + * Configure the host to capture startup errors. |
| 79 | + |
| 80 | +* The [`requestTimeout` attribute](xref:host-and-deploy/iis/web-config#attributes-of-the-aspnetcore-element) doesn't apply to in-process hosting. |
| 81 | + |
| 82 | +* Sharing an app pool among apps isn't supported. Use one app pool per app. |
| 83 | + |
| 84 | +* The architecture (bitness) of the app and installed runtime (x64 or x86) must match the architecture of the app pool. For example, apps published for 32-bit (x86) must have 32-bit enabled for their IIS Application Pools. For more information, see the [Create the IIS site](xref:tutorials/publish-to-iis#create-the-iis-site) section. |
| 85 | + |
| 86 | +* Client disconnects are detected. The [`HttpContext.RequestAborted`](xref:Microsoft.AspNetCore.Http.HttpContext.RequestAborted%2A) cancellation token is cancelled when the client disconnects. |
| 87 | + |
| 88 | +* When hosting in-process, <xref:Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync%2A> isn't called internally to initialize a user. Therefore, an <xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation> implementation used to transform claims after every authentication isn't activated by default. When transforming claims with an <xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation> implementation, call <xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A> to add authentication services: |
| 89 | + |
| 90 | + ```csharp |
| 91 | + public void ConfigureServices(IServiceCollection services) |
| 92 | + { |
| 93 | + services.AddTransient<IClaimsTransformation, ClaimsTransformer>(); |
| 94 | + services.AddAuthentication(IISServerDefaults.AuthenticationScheme); |
| 95 | + } |
| 96 | + |
| 97 | + public void Configure(IApplicationBuilder app) |
| 98 | + { |
| 99 | + app.UseAuthentication(); |
| 100 | + } |
| 101 | + ``` |
| 102 | + |
| 103 | +* [Web Package (single-file) deployments](/aspnet/web-forms/overview/deployment/web-deployment-in-the-enterprise/deploying-web-packages) aren't supported. |
0 commit comments