ASP.NET Core InProcess Hosting

ASP.NET Core InProcess Hosting Model

In this article, I will discuss the ASP.NET Core InProcess Hosting Model with Examples. Please read our previous article discussing the ASP.NET Core Main Method with an Example. 

What is the Hosting?

Hosting refers to the service that allows our web application to be accessible over the Internet. When we develop a web application, we need a server where our application’s files, databases, and other resources can reside and be served to users who access our application via their web browsers.

What is a Web Server?

This is the physical or virtual machine that runs the server software, such as Apache, Nginx, or Microsoft’s Internet Information Services (IIS), where we host our application. The web server handles requests from clients (web browsers), executes the appropriate application logic, and then sends the response back to the client.

What are Hosting Models in ASP.NET Core?

In ASP.NET Core, a hosting model defines how your application is hosted, configured, and served to handle incoming HTTP requests. They determine the relationship between the ASP.NET Core application and the web server, influencing factors such as performance, scalability, and deployment flexibility. There are two primary hosting models in ASP.NET Core:

  • In-Process Hosting
  • Out-Of-Process Hosting

These models determine how the ASP.NET Core application is hosted, which web servers are used, and how requests are processed. Understanding these hosting models is crucial for configuring applications effectively and optimizing their behavior in various environments.

What is the InProcess Hosting Model in ASP.NET Core?

In the InProcess hosting model, the ASP.NET Core application runs inside the IIS worker process (w3wp.exe). This is the default hosting model for ASP.NET Core applications running on Windows OS and in IIS or IIS Express Web Server. The key benefit of InProcess hosting is better performance because there is no need for communication between IIS and an external process.

What is the OutOfProcess Hosting Model?

In the OutOfProcess hosting model, the ASP.NET Core application runs in a separate worker process, mostly in the Kestrel Web Server worker process. In this case, IIS (or any other web server such as Apache or Nginx) acts as a reverse proxy that forwards incoming HTTP requests to an internal web server, which is typically Kestrel. IIS (Apache or Nginx) acts as a reverse proxy, forwarding HTTP requests to the Kestrel server.

How Does the InProcess Hosting Model Work in ASP.NET Core?

Please look at the following diagram to better understand how the InProcess Hosting Model works with an ASP.NET Core Application. Here, we use only one server, IIS, to handle client requests and execute the application code.

How Does the InProcess Hosting Model Work in ASP.NET Core?

The InProcess Hosting Model comes into the picture when we only host the application using IIS on the Windows Operating System. The InProcess Hosting Model works as follows:

Step 1: Internet (User’s Request)

This is where the user interacts with our web application. That means the process begins with the client making a request to the server hosting the application. For example, a user could be browsing a website or calling an API through their browser or some other client. The user’s browser sends an HTTP request to the IIS web server.

Step 2: IIS (Internet Information Services)

IIS is the web server that first receives the incoming request from the client over the internet. Here, it will check whether the application is up and running or not:

  • If the application has not been started, IIS will forward the request to the ASP.NET Core Module (ANCM).
  • If the application is already running, IIS directly forwards the request to the IIS HTTP Server.
Step 3: ASP.NET Core Module (ANCM)

The ASP.NET Core Module (ANCM) is a component within IIS that manages the ASP.NET Core application. It is responsible for loading and starting the ASP.NET Core application inside the IIS worker process (w3wp.exe) if it’s not already running. The following things are going to be done by the ASP.NET Core Module (ANCM):

  • ANCM checks if the ASP.NET Core application is up and running.
  • If the application has not been started yet, ANCM will start it. To start the application, ANCM loads the .NET Core runtime and initializes the ASP.NET Core application within the IIS worker process. This will be done when the application pool starts or the server receives the first request.
  • It then forwards the request to the IIS HTTP Server.
Does the ANCM Module Come into the Picture with Every Client Request?

No. The primary responsibility of the ASP.NET Core Module (ANCM) is to start the ASP.NET Core application during the initial request or when the application pool starts. So, if the application is already up and running, IIS can directly forward the request to the IIS HTTP Server.

Step 4: IIS HTTP Server

This component acts as the HTTP protocol handler. It deals with everything related to the HTTP protocol, such as URL, HTTP method, HTTP Headers, etc. The following things are going to be done by the IIS HTTP Server:

  • The IIS HTTP Server receives the request from the ASP.NET Core Module.
  • It decodes the request and processes the HTTP details (e.g., checks the method: GET, POST, etc., verifies HTTP headers, verifies the HTTP protocol, etc).
  • It sends the processed request to the Application Code by passing HTTP context information (like the URL, headers, etc.) for further handling.
Step 5: Application Code (ASP.NET Core Application)

This is where the business logic and web page generation happen. The ASP.NET Core application has all the required code, like controllers, services, and middleware, to handle the user request. The request goes through the ASP.NET Core Middleware Pipeline, which may include:

  • Authentication/Authorization (e.g., checking if the user is allowed to access a particular resource).
  • Routing (figuring out which Controller and Action should handle the request).
  • Any other custom middleware components you have added (like logging or exception handling).

Finally, the Controller Action processes the request, interacts with the database if needed, prepares a response (such as an HTML page, a JSON response, or other data), and returns that response to the IIS HTTP Server.

Step 6: IIS HTTP Server

After receiving the processed response from the ASP.NET Core application, the IIS HTTP Server sends it back to IIS. So, the response goes from the application code to the IIS HTTP Server and then directly to IIS. ANCM does not handle responses

Step 7: IIS

IIS now takes the response and prepares it to be sent back over the internet to the client (browser, mobile app, etc.) who initially made the request.

Note: The InProcess Hosting Model makes everything faster by having the ASP.NET Core application run inside the same process as IIS (w3wp.exe), avoiding extra steps involved in passing requests between separate processes.

Example to Understand InProcess Hosting Model in ASP.NET Core Application.

When we create a new ASP.NET Core Empty application, the ASP.NET Core framework creates the following Program class with the Main method by default.

Example to Understand InProcess Hosting Model in ASP.NET Core Application

When executing an ASP.NET Core Web Application, the .NET Core Runtime looks for the Main() method. The Main() method is the entry point for the .NET Core Application to execute. As you can see in the above code, the Main() method of the Program class calls the static CreateBuilder() method.

What are the Tasks Performed by the CreateBuilder() Method in ASP.NET Core?

The CreateBuilder() method sets up the Web Host to host the ASP.NET Core application and configure the required services using default preconfigured settings. As part of setting up the Web Host and configuring services, the CreateBuilder() method performs several key tasks. Some of the important tasks are as follows:

  • Set up Web Server (IIS, IIS Express, or Kestrel)
  • Host the Application (In-Process or Out-Of-Process)
  • Logging (debugging and console logging)
  • Configuration (How to access the Data from Configuration Files)
  • Dependency Injection Container (registering built-in and custom services)
What exactly does the CreateBuilder() method do to set up the Web Host?

The web host runs the ASP.NET Core application. It handles HTTP requests, listens to configured URLs, and forwards requests to the application’s middleware pipeline. Let us understand what exactly the CreateBuilder() method does to configure and set up the Web Host. The CreateBuilder() method Configures the Web Host with default settings to host the application. Some of the key configurations are:

  • Kestrel Configuration: Kestrel is configured as the default web server and is optimized for serving ASP.NET Core applications.
  • IIS Integration: If the application is hosted on Windows, it sets up IIS integration to ensure compatibility with IIS or IIS Express.
  • Environment: It also configures the server’s environment (e.g., development, staging, production) and URLs the app will listen on.
  • Hosting Model: It also configures whether to use InProcess or Out of Process hosting models.

From the hosting model point of view, an ASP.NET Core Web application can be hosted in two ways: in-Process Hosting or out-of-Process Hosting. Let us first discuss the in-Process Hosting Model and then the out-of-Process Hosting Model.

How Do We Configure InProcess Hosting in ASP.NET Core?

When we create a new ASP.NET Core Web Application using any project template in .NET 8, by default, the project is configured to use the InProcess Hosting Model if the application is intended to be hosted in IIS or IIS Express. The InProcess Hosting Model performs better than the OutOfProcess Hosting Model because the ASP.NET Core application runs within the same IIS worker process (w3wp.exe), which reduces overhead by avoiding cross-process communication.

Note: With IIS or IIS Express, we can also configure the out-of-process hosting model, which we will discuss later.

Method 1: Using Project Properties File

To Manually configure the InProcess Hosting for the ASP.NET Core Web application, there is only one simple setting: add the <AspNetCoreHostingModel> element to the application project file with a value of InProcess. To do so, right-click on your project from the solution explorer and click the Edit Project File option from the context menu, as shown in the code below.

How Do We Configure InProcess Hosting in ASP.NET Core?

Once you open the Application Project file, modify it as shown below. As you can see, we have added the <AspNetCoreHostingModel> element and set its value to InProcess. The other possible value for this element is OutOfProcess.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>
</Project>
Method 2:  Specify the Hosting Model as InProcess using GUI

To do so, right-click on your project in the Solution Explorer and click on the Properties option from the context menu, which will open the Project Properties window. Select the Debug tab from this window and click the Open debug launch profiles UI button, as shown in the image below.

Specify the Hosting Model as InProcess using GUI

Once you click on the Open debug launch profiles UI button, the Launch Profile window will open. From this window, select IIS Express and scroll down. You will find the Hosting Model drop-down list with the following values somewhere. As we have already set InProcess as our hosting model in the Project Properties window, you will see that the In-Process Hosting Model has been selected as the default hosting model here. Whatever hosting model you want to use, you can also change it here.

ASP.NET Core InProcess Hosting Model

What Happens When We Set the Hosting Model as InProcess?

When configuring the hosting model as InProcess and running the application using the IIS Express launch profile, the ASP.NET Core application runs directly within the IIS Worker Process (w3wp.exe or iisexpress.exe in Development). This integration is more efficient than running the application in a separate process because it eliminates the need for inter-process communication between the web server and the application.

Because the application runs in the same process as IIS, there is no additional overhead from network delays or context switching between the web server and the application. This can lead to better response times and throughput, making it ideal for high-performance-sensitive environments.

The process name used to execute the application code is w3wp in the case of IIS. Similarly, if it is IIS Express, the process name will be iisexpress.

How Do We Use InProcess Hosting Model to Run Applications?

To use the InProcess Hosting Model in Visual Studio, we first need to set the launch profile to IIS Express, as shown in the image below.

How Do We Use InProcess Hosting Model to Run Applications?

Then, we need to set the AspNetCoreHostingModel element value to InProcess within the Project Properties file and run the application. You will get the following output:

How Do We Use InProcess Hosting Model to Run Applications?

You are getting the above message from the following MapGet method, which is present inside the Main method of the Program class, as shown in the image below.

What are Hosting Models in ASP.NET Core?

To display the name of the process that executes our application code in the browser, we need to use the System.Diagnostics.Process.GetCurrentProcess().ProcessName statement within the Main method of the Program class. So, modify the Main method of the Program class as follows.

namespace FirstCoreWebApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            var app = builder.Build();

            app.MapGet("/", () => "Worker Process Name : " + System.Diagnostics.Process.GetCurrentProcess().ProcessName);

            app.Run();
        }
    }
}

Now, run the application from Visual Studio using the IIS Express profile. It should display the following message in the browser, as shown in the image below. This ensures that the IIS Worker Process executes the application code.

ASP.NET Core InProcess Hosting Model with Examples

You can also verify whether it is using a Microsoft IIS Server by using the browser developer tool. Run the application and open the browser developer tool by pressing the F12 key. Then, go to the Network tab and issue the same request again. You will see that it is showing Microsoft IIS as the server, as shown in the image below.

ASP.NET Core InProcess Hosting Model with Examples

What is IIS Express?

IIS Express is a lightweight, self-contained version of Internet Information Services (IIS) designed for developers to use on their local development machines. It provides a web server environment that is similar to the full version of IIS, making it easier to develop and test web applications locally before deploying them to production servers.

The most important point to remember is that we use IIS Express only in development, not in production. We need to use IIS in production. In our upcoming session, we will discuss deploying an ASP.NET Core Application on IIS.

Next=> So, before understanding the Out-of-Process Hosting Model to host our ASP.NET Core Web Application, we first need to understand what the Kestrel Web Server is and How It works. Then, we will discuss the Out-Of-Process Hosting Model.

In the next article, I will discuss the Kestrel Web Server in ASP.NET Core Application with Examples. In this article, I explain ASP.NET Core InProcess Hosting with examples. I hope you enjoy this ASP.NET Core InProcess Hosting Model article. 

16 thoughts on “ASP.NET Core InProcess Hosting”

  1. Also, this article is missing some points from content guideline at the top of this article:

    – What is InProcess hosting in ASP.NET Core?
    – How does the InProcess hosting work in ASP.NET Core?
    – How to host ASP.NET Core Application using InProcess hosting Model?

    1. Everything is there in the document. Yes, proper headings are not there. For your queries, please find the answers below.

      What is InProcess hosting in ASP.NET Core? // How does the InProcess hosting work in ASP.NET Core?
      In case of InProcess hosting (i.e. when the CreateDefaultBuilder() sees the value as InProcess for the AspNetCoreHostingModel element in the project file), behind the scene the CreateDefaultBuilder() method internally calls the UseIIS() method. Then host the application inside the IIS worker process (i.e. w3wp.exe for IIS and iisexpress.exe for IISExpress). In the InProcess hosting model, there is only one web server i.e. IIS which will handle the request and response.

      How to host ASP.NET Core Application using InProcess hosting Model?
      To configure the InProcess hosting for ASP.NET Core Web application there is only one simple setting, just add the element to the application project file with a value of InProcess.

Leave a Reply

Your email address will not be published. Required fields are marked *