IIS lifecycle for any .net application
When a new request comes into an ASP.NET application hosted in Internet Information Services (IIS), it goes through a well-defined lifecycle. Here's a breakdown of the key stages:
- Client Sends Request:
- A user (client) initiates an HTTP request (e.g., by typing a URL in their browser or clicking a link).
- This request is sent over the network to the IIS server.
- HTTP .sys Receives Request (Kernel Mode):
- HTTP.sys is the HTTP listener in the Windows kernel. It's the first component to receive the incoming HTTP request.
- It manages the TCP/IP connections and listens for requests on configured ports (usually 80 for HTTP and 443 for HTTPS).
- HTTP.sys performs some initial processing, such as validating the request format.
- Request Queuing:
- HTTP.sys determines the appropriate application pool for the requested application based on the configured bindings (IP address, port, hostname).
- The request is then placed in a request queue associated with that application pool.
- Worker Process Picks Up Request (User Mode):
- Worker processes (w3wp.exe) in the application pool are responsible for processing the requests in their queue.
- When a worker process is free, it picks up the next request from the queue. If no worker process is available, HTTP.sys may signal the World Wide Web Publishing Service (WWW Service) to start a new one (depending on configuration).
- ASP.NET ISAPI Extension (aspnet_isapi.dll):
- For ASP.NET applications (both Framework and Core, though the implementation details differ), IIS uses an ISAPI (Internet Server Application Programming Interface) extension to hand off the request to the .NET runtime.
- For older ASP.NET Framework, aspnet_isapi.dll is the primary ISAPI extension.
- For ASP.NET Core, the ASP.NET Core Module acts as the native IIS module that integrates with the .NET Core runtime. It can either host the .NET Core runtime in-process or forward requests to a separate Kestrel server (out-of-process).
- ASP.NET Runtime Processing:
- ASP.NET Framework:
- The aspnet_isapi.dll passes the request to the .NET Framework runtime within the worker process.
- An HttpRuntime object is created, which is the entry point for ASP.NET processing.
- Core objects like HttpContext, HttpRequest, and HttpResponse are created to represent the current request and response.
- The request goes through the HTTP Pipeline, which consists of a series of HTTP Modules and finally an HTTP Handler.
- HTTP Modules are components that can intercept and process the request at various stages (e.g., authentication, authorization, session management, logging). They are configured in web.config.
- The HTTP Handler is the endpoint responsible for processing the specific request and generating the response (e.g., an .aspx page handler, an .ashx handler).
- For an .aspx page, the ASP.NET Page Lifecycle begins within the handler.
- ASP.NET Core:
- In-Process Hosting: The ASP.NET Core Module loads the CoreCLR and runs the application within the IIS worker process. The request goes directly into the ASP.NET Core Middleware Pipeline.
- Out-of-Process Hosting: The ASP.NET Core Module forwards the request to the backend Kestrel server (a cross-platform web server). Kestrel then processes the request through the ASP.NET Core Middleware Pipeline.
- The ASP.NET Core Middleware Pipeline is a sequence of middleware components that are configured in Startup.cs. Each middleware can perform operations on the HttpContext and either pass the request to the next middleware in the pipeline or short-circuit the request.
- Routing middleware matches the request URL to a specific endpoint (e.g., a Controller action or a Razor Page).
- MVC or Razor Pages middleware then handle the request, execute application logic, and prepare the response.
- ASP.NET Framework:
- Response Generation:
- The HTTP Handler (in ASP.NET Framework) or the end of the Middleware Pipeline (in ASP.NET Core) generates the HTTP response, which typically includes HTML, JSON, XML, or other content.
- The HttpResponse object contains the response data, headers, and status code.
- Response Sent Back to Client:
- The ASP.NET runtime passes the response back to IIS.
- IIS forwards the response to HTTP.sys.
- HTTP.sys sends the HTTP response back to the client's browser over the network.