Think with Enlab

Diving deep into the ocean of technology

Stay Connected. No spam!

How IIS Processes ASP.NET Core HTTP Request

 

Have you ever wondered what happens under the hood when you make an API call to your ASP.NET Core application hosted in IIS? Your request is passed through 2 pipelines in IIS and ASP.NET Core to be processed before returning the response. This article explains how your request is processed and how you can use it to add more functionalities to your ASP.NET Core applications.

 

HTTP pipeline

According to Microsoft documentation about IIS architecture, the HTTP request is picked up from the kernel mode, passed to the user mode for processing that results in a new IIS worker process (w3wp.exe) serving that HTTP request. Here are the steps:

 

 

  1. When a client browser initiates an HTTP request for a resource on the webserver, HTTP.sys intercepts the request

  2. HTTP.sys contacts the WAS to obtain information from the configuration store

  3. WAS requests configuration information from the configuration store, applicationHost.config

  4. The WWW Service receives configuration information, such as application pool and site configuration

  5. The WWW Service uses the configuration information to configure HTTP.sys

  6. WAS starts a worker process for the application pool to which the request was made

  7. The worker process processes the request and returns a response to HTTP.sys

  8. The client receives a response

Source: Introduction to IIS Architecture - Microsoft documentation

 

After step 6, your ASP.NET core code is executed by either the IIS worker process itself or by a Kestrel server. The HTTP response is returned to HTTP.sys once done processing. HTTP.sys then returns to the client browser. 

Microsoft uses ASP.NET Core Module, a native IIS module, to plug into the pipeline to either host your ASP.NET Core app inside the IIS worker process (in-process hosting model) or to forward HTTP requests to a backend ASP.NET Core app running the Kestrel server (out-of-process hosting model).

 

In-process hosting model

The in-process hosting model is the default hosting model for all apps built with ASP.NET Core 2.2 or later. In the in-process hosting model, what ASP.NET Core Module does is to load the CoreCLR and calls the Program.Main method to bootstrap your app’s logic. It then handles the lifetime of the IIS native request.

 

Source: Host ASP.NET Core on Windows with IIS - Microsoft documentation

 

  • From the above diagram, it is clear that your app’s logic takes in the HttpContext produced by IISHttpServer which is responsible for converting the native HTTP request to managed before passing the ASP.NET Core middleware pipeline. 

  • The middleware pipeline handles the request and passes it on as an HttpContext instance to your app’s logic. 

  • The IISHttpServer passes your app’s response back to IIS which then forwards it back to the client initiating the request. 

All of this happens right in the same IIS worker process, so the best performance is achieved.

Make sure to call the UseIIS method when configuring your application, and to explicitly specify the in-process hosting model in the web.config file as below:

<system.webServer>
<aspNetCore processPath="dotnet" hostingModel="InProcess" />
</system.webServer>

 

To verify, check the response header to make sure it’s Microsoft-IIS, not Kestrel.

 

 

Out-of-process hosting model

Unlike the in-process hosting model, in the out-of-process hosting model, your app’s logic runs in a dotnet.exe process separate from the IIS worker process as follows:

 

Source: Host ASP.NET Core on Windows with IIS - Microsoft documentation

 

  • The ASP.NET Core Module handles the dotnet.exe process management and forwards the HTTP request to the Kestrel server. 

  • The Kestrel server picks up the request from ASP.NET Core Module to forward into the ASP.NET Core middleware pipeline. 

  • The middleware pipeline handles the request and passes it on as an HttpContext instance to your app’s logic. 

  • The Kestrel receives the app’s response to pass back to IIS which forwards it back to the client initiating the request. 

To deploy your application using an out-of-process hosting model, specify it in your web.conf file as below:

<system.webServer>
<aspNetCore processPath="dotnet" hostingModel="OutOfProcess" />
</system.webServer>

 

Check the response header, it should be Kestrel.

 

 

ASP.NET core middleware pipeline

Once the request is passed to your app’s logic as an HttpContext instance, it undergoes the ASP.NET Core middleware pipeline as depicted in the below diagram where you write custom code to handle the request and return your expected response.

 

Source: ASP.NET Core Middleware - Microsoft documentation

 

Middleware is a software component that can be written and plugged into the pipeline for processing your app’s logic before or after passing the request to the next component. 

Many built-in middleware components are ready to use. Understanding what they are for and their orders to use is important.

 

Final thoughts

To be considered as a serious ASP.NET developer, understanding the HTTP pipeline and ASP.NET Core Middleware pipeline is crucial. Therefore, I was trying to understand the fundamentals and connect the dots. This effort brought me some insights that hopefully will help you shorten your research journey.

 

 

References

About the author

Vinh Tran

Hi, my name's Vinh and I'm the CEO of Enlab Software. I'm passionate about building world-class digital products, helping my team become mature in their careers, and creating an environment where people find purpose and meaning in life.
Frequently Asked Questions (FAQs)
What are the main stages of IIS request processing for ASP.NET Core applications?

IIS request processing involves several stages: the HTTP request is first intercepted by HTTP.sys and passed to the Windows Activation Service (WAS). The WAS then starts a worker process for the application pool, and this worker process handles the request. Finally, the response is sent back to the client through HTTP.sys.

How does the in-process hosting model work in IIS for ASP.NET Core applications?

In the in-process hosting model, the ASP.NET Core Module loads the CoreCLR and calls Program.Main to start the application. The request handling occurs within the same IIS worker process, ensuring optimal performance. This model requires the application to be configured with the UseIIS method and to specify the in-process hosting model in the web.config file.

 

What is the role of the ASP.NET Core Module in the IIS request processing pipeline?

The ASP.NET Core Module is a native IIS module that plugs into the IIS pipeline. It is responsible for starting the application, managing its lifecycle, and either hosting the application inside the IIS worker process (in-process hosting) or forwarding HTTP requests to a backend ASP.NET Core app running the Kestrel server (out-of-process hosting).

How does the out-of-process hosting model differ from the in-process model in IIS?

In the out-of-process hosting model, the application’s logic runs in a separate process from the IIS worker process. The ASP.NET Core Module forwards the HTTP request to the Kestrel server, which then processes the request through the ASP.NET Core middleware pipeline. The response is sent back to the client through IIS. This model is configured in the web.config file and is identified by the response header indicating “Kestrel” instead of “Microsoft-IIS.”

What is the role of middleware in the ASP.NET Core request processing pipeline, and how does it integrate with IIS?

Middleware components in ASP.NET Core are software components that handle requests and responses in the application pipeline. Each piece of middleware can perform operations before and after the next component in the pipeline. When integrated with IIS, the middleware pipeline takes over after the ASP.NET Core Module has forwarded the request to the application. It allows developers to customize request processing, authentication, routing, and other tasks by configuring the order and inclusion of different middleware components.

Up Next

July 05, 2024 by Dat Le
In the rapidly evolving world of software development, Big Data stands out as a transformative force....
June 27, 2024 by Dat Le
In today's rapidly evolving digital landscape, secure coding practices are paramount to safeguarding applications from a...
June 20, 2024 by Dat Le
In the rapidly evolving digital landscape, the role of User Interface (UI) and User Experience (UX)...
June 17, 2024 by Dat Le
In the dynamic world of software development, one element has emerged as crucial to success: User...

Can we send you our next blog posts? Only the best stuffs.

Subscribe