ASP.NET Core OutOfProcess Hosting

ASP.NET Core Out of Process Hosting

In this article, I am going to discuss ASP.NET Core OutOfProcess Hosting Model with Examples. I strongly recommend you read ASP.NET Core InProcess Hosting and Kestrel Web Server in ASP.NET Core Web Application articles before proceeding to this article. As part of this article, we are going to discuss the following pointers in detail.

  1. What is ASP.NET Core InProcess Hosting Model?
  2. How to Configure the OutofProcess Hosting in ASP.NET Core Application?
  3. What is Out of Process Hosting in ASP.NET Core?
  4. Why do we need a Reverse Proxy Server?
  5. What happens when we run the ASP.NET Core Application using .NET Core CLI?
  6. Can we run an ASP.NET Core Web Application without using the Built-in Kestrel Web Server?

Before understanding OutOfProcess Hosting Model in ASP.NET Core Web Application, let us first have a look at the InProcess hosting model and understand how it works.

What is ASP.NET Core InProcess Hosting Model?

Let’s first have a look at InProcess Hosting Model before proceeding to the Out of Process Hosting Model in ASP.NET Core Web Application. As we already discussed, by default, if we use IIS Express Profile while launching the application, then it will use In Process Hosting, and if we use the Application Name as the Launch Profile in Visual Studio, then it will use the Out of Process Hosting using Kestrel Server.

We can enable the InProcess Hosting Model in the Application Project file. So, we need to add <AspNetCoreHostingModel> element and set its value to InProcess with the Application Project file, as shown in the below image.

What is ASP.NET Core InProcess Hosting Model?

In ASP.NET Core, with InProcess Hosting Model, our application is going to be hosted in the IIS worker process. The most important point you need to remember is that we have only one web server, i.e., IIS Server, in the case of InProcess Hosting, which will host our application, as shown in the image below.

What is ASP.NET Core InProcess Hosting Model?

How to Configure the 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 setting OutOfProcess Hosting in ASP.NET Core

In this case, you 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>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>    
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>    
  </PropertyGroup>
</Project>
Method 2:  Specify the Hosting Model as OutOfProcess in the Project Properties

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 on the Open debug launch profiles UI button, as shown in the image below.

Specify the Hosting Model as OutOfProcess in the Project Properties

Once you click on the Open debug launch profiles UI button, then it will open the Launch Profile window, and from this window, select IIS Express and scroll down, and 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 is selected as the default hosting model. Whatever hosting model you want to use, you can also change it here.

How to Configure the OutofProcess Hosting in ASP.NET Core Application?

What is Out of Process Hosting in ASP.NET Core?

In ASP.NET Core, hosting refers to the process of running your web application and making it accessible over the internet or intranet. OutOfProcess hosting is one of the hosting options available in ASP.NET Core, which involves running your application in a separate process from the web server.

In the OutOfProcess hosting model, the web server (like IIS or Nginx) communicates with your ASP.NET Core application through a network protocol, usually HTTP. The web server acts as a reverse proxy, forwarding incoming HTTP requests to the ASP.NET Core application process, which then processes the requests and sends back responses to the web server, which in turn forwards them to the client.

In the case of the ASP.NET Core OutOfProcess Hosting Model, there are two web servers.

  1. An Internal Web Server, which is the Kestrel Web Server
  2. And an External Web Server can be IIS, Apache, or Nginx.

The most important point you need to remember is that depending on how you are running your application with the OutOfProcess hosting model, the external web server (i.e., IIS, Apache, and Nginx) may or may not be used.  

As we already discussed, the Kestrel web server is a cross-platform web server already embedded with your ASP.NET Core application. So, if you are using the Out of Process Hosting model to run your ASP.NET Core application, then the Kestrel web server can be used in one of the following ways. 

Method 1: Kestrel as Internet-Facing Web Server or 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, only the Kestrel Server is used, and the other one, i.e., external web server (i.e., IIS, Apache, and Nginx), is not going to be used. So, when we run the application using the .NET core CLI, Kestrel is the only Web Server that will be used to handle and process the incoming HTTP request, as shown in the below image. 

ASP.NET Core OutOfProcess Hosting Model with Examples

To confirm this, open the command prompt and run the application as shown in the below image. First, you need to change the directory to the folder which contains your ASP.NET Core application. My project is present in the “D:\Projects\FirstCoreWebApplication\FirstCoreWebApplication” folder, so I change the current directory to my project file and then use the dotnet run command.

What is Out of Process Hosting in ASP.NET Core?

Once you type the dotnet run command and press the enter key, it will build, host, and run the application, as shown in the below image.

What is Out of Process Hosting in ASP.NET Core?

Now open the browser and navigate to any of the above URLs shown in the command prompt, and you will get the following output.

What is Out of Process Hosting in ASP.NET Core?

So, in this case, Kestrel is the only server that will handle and process the incoming HTTP Request. The following code of the Main Method of the Program class displays the worker process name.

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();
        }
    }
}

You can verify the same using the browser developer tool, as shown in the image below.

ASP.NET Core OutOfProcess Hosting Model with Examples

Method2: Kestrel as the Internal Web Server

The Kestrel can be used as the Internal Web Server. That means the Kestrel Web Server can also be used along with a Reverse Proxy Server such as IIS, Apache, or Nginx. Now the question that should come to your mind is, If Kestrel can be used as a Web Server that can directly handle and process the incoming HTTP Request, then why do we need a Reverse Proxy Server?

Why do we need a Reverse Proxy Server?

This is because the Reverse Proxy Server provides an additional layer of configuration and security which is not available with the Kestrel Server. It also maintains load balancing. So, a good choice is to use Kestrel Web Server and a Reverse Proxy Server such as IIS, Apache, or Nginx.

So, when we use 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, 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.

How OutOFProcess Hosting Model Work in ASP.NET Core

In our upcoming articles, we will discuss deploying an ASP.NET Core application to IIS and using IIS as a Reverse Proxy Server. 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.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>    
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>    
  </PropertyGroup>
</Project> 

As we have configured the Out of Process Hosting Model, now the IIS Express acts as the Reverse Proxy Server, and Kestrel acts as the Internal Web Server when we use IIS Express as the launch profile to run the application.

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

Now run the application using the FirstCoreWebApplication profile, and you will see the worker process as your project name. So, when you are using the Out of Process Hosting model, the Kestrel Web Server will host the application and process the request irrespective of whether you are using a Reverse Proxy Server.

Note: With Out Of Process Hosting Model in Place, if we run the application using IIS Express Profile, then IIS Express Server will act as the External Web Server, and Kestrel act as the Internal Web Server. On the other hand, if we run the application using the FirstCoreWebApplication profile, then Kestrel is the only server that will host the application and Handle the Incoming HTTP Requests.

What happens when we run the ASP.NET Core Application using .NET Core CLI?

When running our application using .NET Core CLI, then by default, it ignores the hosting setting you specified in the application’s project file, i.e., the CSPROJ file. So, in that case, the AspNetCoreHostingModel element value will be ignored. The .NET Core CLI always uses OutOfProcess Hosting Model, and Kestrel is the only Web Server that will host the ASP.NET Core Application as well as handles the HTTP requests. 

Can we run an ASP.NET Core Web Application without using the Built-in Kestrel Web Server?

YES. 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. 

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

Note: The point that you need to remember is with In Process Hosting Model, IIS is the Only Web Server that is going to host the application and handle the incoming HTTP Requests. On the other hand, with Out Of Process Hosting Model, we can use only Kestrel as the Web Server, which can host and handle the incoming HTTP Requests as well, as we can use Kestrel plus any one of the IIS, Apache, and Nginx Web Server to Host and handle the incoming HTTP Requests. In this case, Kestrel will be used as the Internal Server, and either IIS, Apache, or Nginx Web Server will be the External Web Server.

Benefits of OutOfProcess Hosting:
  1. Isolation: Since the ASP.NET Core application runs in a separate process from the web server, it provides strong isolation. This isolation can be particularly useful for security, stability, and management purposes.
  2. Runtime Independence: You can choose a different runtime (like .NET Core or .NET 6) for your ASP.NET Core application than the web server uses, allowing for version and framework flexibility.
  3. Scalability: The application can be scaled independently of the web server. Multiple instances of the application can run and be load-balanced to handle high traffic.
  4. Multi-Platform: OutOfProcess hosting is platform-agnostic, meaning you can host your ASP.NET Core application on various web servers and operating systems.
  5. Upgrade Flexibility: You can upgrade the ASP.NET Core application independently of the web server, which can help in adopting new features, bug fixes, or even completely new versions of the framework.
  6. Crash Isolation: If your ASP.NET Core application crashes, it doesn’t affect the web server or other applications running on the server.
When to use OutOfProcess Hosting Model in ASP.NET Core MVC?

It’s important to note that while OutOfProcess hosting provides benefits like isolation and runtime independence, it also introduces a bit of performance overhead compared to InProcess hosting. The communication between the web server and the application process involves network calls, serialization, and deserialization of data, which can impact response times, especially for high-traffic applications.

OutOfProcess hosting is suitable for scenarios with critical requirements for strong isolation, runtime independence, and upgrade flexibility. It’s commonly used when deploying ASP.NET Core applications to a production environment or when working with external web servers like Nginx or Apache.

In the next article, I am going to discuss the ASP.NET Core LaunchSettings.json file with Examples. Here, 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 OutOfProcess Hosting”

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

  2. 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. 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 *