ASP.NET Core Out Of Process Hosting

ASP.NET Core Out of Process Hosting

In this article, I will discuss the ASP.NET Core Out-of-Process Hosting Model with Examples. Before proceeding to this article, I strongly recommend you read the ASP.NET Core InProcess Hosting and Kestrel Web Server in ASP.NET Core Web Application articles. We will also discuss the following pointers in detail.

  1. What is Out of Process Hosting in ASP.NET Core?
  2. How Do We Configure OutofProcess Hosting in ASP.NET Core Application?
  3. How Do We Use Out-of-Process Hosting in ASP.NET Core?
  4. Kestrel as Internet-Facing Web Server
  5. What is the Role of the IIS Worker Process?
  6. What is the Role of the Kestrel Web Server Process?
  7. When and Which Server is Used to Host and Process the Application?
What is Out of Process Hosting in ASP.NET Core?

The ASP.NET Core Out-of-Process hosting model hosts the ASP.NET Core application in a worker process separate from the web server worker process that handles incoming HTTP requests. This is different from the In-Process hosting model, where the ASP.NET Core application runs inside the same process as the web server.

The out-of-process hosting model uses Kestrel as the webserver to run the application code, with another web server (such as IIS, Nginx, or Apache) acting as a reverse proxy, which will receive the incoming request from the client and then forward the requests to the Kestrel server. So, in the case of the ASP.NET Core Out-of-Process Hosting Model, there are two web servers.

  • An internal web server, i.e., the Kestrel web server.
  • An external web server can be IIS, Apache, Nginx, etc.
How Do We Configure OutofProcess Hosting in ASP.NET Core Application?

We can configure the Out-of-Process Hosting in two ways in ASP.NET Core.

Method 1: Using Project Properties File

In this case, we need to add the <AspNetCoreHostingModel> element to the applications project file with a value of OutOfProcess. So, open the Project Properties file and copy and paste the following code.

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

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

Specify the Hosting Model as OutOfProcess 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. Somewhere, you will find the Hosting Model drop-down list with the following values. As we have already set OutOfProcess as our hosting model in the Project Properties window, you will see that the Out of Process Hosting Model has been selected as the default hosting model.

How Do We Configure OutofProcess Hosting in ASP.NET Core Application?

How Do We Use Out-of-Process Hosting in ASP.NET Core?

The point that you need to remember is that if we use the HTTP or HTTPS profiles to run our application, neither the InProcess nor the OutOfProcess hosting model will come into the picture. In that case, Kestrel is the only web server host and runs the application. The InProcess and OutOfProcess hosting models will come into the picture when we launch the application using IIS.

With the In-Process Hosting Model, IIS is the only web server that will host the application and handle the incoming HTTP requests. On the other hand, with the Out-of-Process Hosting Model, IIS will be the External Web Server, which will receive the incoming HTTP requests from the client, and Kestrel will be the Internal Web Server, which will process the request by executing the application code. In this case, IIS will forward the incoming request to the Kestrel Web Server.

Kestrel as Internet-Facing Web Server (External Web Server)

We can use the Kestrel Web Server as the Internet-Facing web server, which will directly process the incoming HTTP requests. In this scenario, the Kestrel Server is the only web server that is going to host the application and handle the incoming HTTP Requests by executing the application. The external web server (i.e., IIS, Apache, and Nginx) will not be used.

So, when we run the application using an HTTP or HTTPS profile, Kestrel is the only Web Server that will handle the incoming HTTP request, as shown in the image below. Here, dotnet.exe is nothing but the worker process name for the Kestrel Web Server; for example, if the project name is FirstCoreWebApplication, it will be FirstCoreWebApplication.exe.

Kestrel as Internet-Facing Web Server

To confirm this, run the application using either the HTTP or HTTPS profiles and verify the worker process name, as shown in the image below. Here, it shows FirstCoreWebApplication, which means Kestrel Server is executing our application code.

How Do We Use Out-of-Process Hosting in ASP.NET Core?

Now, you can also verify the server that processes the request using the browser developer tool, as shown in the image below. Kestrel is the Web Server that handles the request.

How Do We Use Out-of-Process Hosting in ASP.NET Core?

This proves that when we use the HTTP or HTTPS profile to launch our application, Kestrel is the only Web Server that hosts our application and executes the application code. In this case, Kestrel works as an Internet-facing Web Server.

Kestrel as Internal Web Server

Kestrel can also be used as an Internal Web Server. That means it can be used with a Reverse Proxy Server (Internet Facing Server) such as IIS, Apache, or Nginx. Now, the question that should come to your mind is, if Kestrel can be used by itself as a Web Server that can directly handle and process the incoming HTTP Request, then why do we need a Reverse Proxy Server?

This is because the Reverse Proxy Server provides an additional layer of configuration, load balancing, URL Rewriting, and security, which is not available with the Kestrel Server. The Kestrel Server’s main job is to execute the application code in Cross-Platform. So, it is a good choice to use the Kestrel Web Server along with a Reverse Proxy Server such as IIS, Apache, or Nginx.

So, when we use the Kestrel Web Server along with a Reverse Proxy Server, the Reverse Proxy Server will receive the incoming HTTP requests from the client and then forward that request to the Kestrel Web Server for processing. Once the Kestrel Server receives the request, it will process the request with its own worker process, generate the response, and send the response back to the Reverse Proxy Server, which then sends the response back to the requested client over the Internet who initially made the request. For a better understanding, please have a look at the following diagram.

blank

The flow of the ASP.NET Core Out of Process Hosting Model is as follows:

  • A client sends a request to IIS.
  • IIS uses the ASP.NET Core Module to forward the request to Kestrel.
  • Kestrel runs the application code to handle the request.
  • The application processes the request and generates a response.
  • Kestrel sends the response back to IIS.
  • IIS will forward the response to the client.
What is the Role of the IIS Worker Process?

The IIS Worker Process handles the initial part of the request lifecycle. This means it provides basic web server functionalities such as SSL, authentication, and serving static files (if configured to do so). It can also manage URL rewriting and other web server-level configuration, load balancing, and security tasks.

After processing the initial request and applying the necessary server-level configurations or security measures, the request is forwarded to the Kestrel server via the ASP.NET Core Module.

What is the Role of the Kestrel Web Server Process?

Kestrel Server’s worker process executes the application code, including everything from processing requests to handling routes to executing middleware components to running controllers and other application logic.

After the application code processes, Kestrel generates the HTTP response. This includes setting response headers and status codes and forming the response body based on the application logic’s output. Once the response is prepared, Kestrel sends it back to the IIS Worker Process through the ASP.NET Core Module. IIS then forwards this response to the client.

Kestrel as an Internal Web Server:

Now, set the hosting model to OutOfProcess in the project properties file and run the application using the IIS Express profile. As shown in the image below, you will see the worker process as your project name. You can also verify in the taskbar that the IIS Server is running.

blank

With the Out-of-Process Hosting Model, when we use IIS Express as the launch profile to run the application, IIS Express acts as the Reverse Proxy Server (Internet Facing Server), and Kestrel acts as the internal web server.

Now, IIS Express receives the incoming HTTP request and then forwards it to the Kestrel Web Server for processing. The Kestrel Web Server processes the request, generates the response, and sends it back to IIS Express, which in turn sends the response back to the client, i.e., to the browser.

When and Which Server is Used to Host and Process the Application?

To better understand When and Which Server is used to Host and Process the ASP.NET Core Web Application, please look at the following Diagram.

When and Which Server is Used to Host and Process the Application?

In the next article, I will discuss the ASP.NET Core LaunchSettings.json file with Examples. In this article, I try to explain the ASP.NET Core OutOfProcess Hosting Model with examples. I hope this article will help you to understand the OutOfProcess Hosting Model in ASP.NET Core Web Applications.

7 thoughts on “ASP.NET Core Out Of Process Hosting”

  1. blank

    As of .Net Core 3.1, the default hosting model is InProcess whenever you create a new application using existing template.

  2. blank

    With reference to this statement : “When we use the InProcess Hosting model, then the application is hosted inside the IIS worker process i.e. w3wp.exe in the case of IIS and iisexpress.exe in the case of IIS Express. That means the Kestrel Web Server is not used with the InProcess hosting model.”

    I still have the option to host the application using kresetel in Visual Studio. How does that work then ?

  3. blank

    When we run the application directly from Visual Studio, then by default Visual Studio uses IIS Express. Now change the AspNetCoreHostingModel element value as shown below in the application’s project file.

    OutOfProcess

    As we have configured the Out of Process hosting model, now the IIS Express acts as a reverse proxy server and Kestrel acts as the internal webserver.

    I think there is no reverse proxy server in this case,cause there is no iis express process in task manager

Leave a Reply

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