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 Examples. As part of this article, we will discuss the following pointers in detail.

  1. ASP.NET Core Hosting Models
  2. What is the InProcess Hosting Model in ASP.NET Core?
  3. How Does the InProcess Hosting Model Work in ASP.NET Core?
  4. Example to Understand InProcess Hosting Model in ASP.NET Core Application
  5. What are the Tasks Performed by the CreateBuilder() Method in ASP.NET Core?
  6. How to Configure InProcess Hosting in ASP.NET Core?
  7. What Happens When We Set the Hosting Model as InProcess?
  8. How to use InProcess Hosting to Run Application?
  9. What is IIS Express?
  10. Why InProcess Hosting Gives Better Performance than the OutOfProcess Hosting Model?
What is the Hosting Model in ASP.NET Core?

In ASP.NET Core, the “Hosting Model” refers to how the application is hosted and executed. The hosting model defines how the application starts, requests are processed, and responses are returned to clients. ASP.NET Core has two types of Hosting Models: InProcess and OutOfProcess. In this article, I will discuss the InProcess Hosting Model in detail, and in our upcoming articles, we will discuss the OutOfProcess Hosting Model.

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

In the case of the InProcess Hosting Model, the ASP.NET Core Web Application will be hosted inside of the IIS Worker Process, i.e., w3wp.exe, and IIS is the only Web Server going to handle the Incoming HTTP Request.

In ASP.NET Core, hosting refers to the process of running your web application and making it accessible over the internet or intranet. InProcess hosting is one of the hosting options available in ASP.NET Core, which allows you to run your application within the same process as the IIS (Internet Information Services) web server.

In InProcess hosting, the ASP.NET Core application runs as a part of the IIS worker process. The application code and the IIS worker process share the same memory space. This hosting model performs better than OutOfProcess hosting because it eliminates communication overhead between the web server and the application process.

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

For a better understanding of how the InProcess Hosting Model works, please look at the following diagram. Here, we only use one server, i.e., IIS, to handle client requests.

How InProcess Hosting Model Work in ASP.NET Core?

The InProcess Hosting Model works as follows:

  1. A request came from the user to IIS on the website’s configured port through the internet over HTTP or HTTPS protocol. Usually 80 (HTTP) or 443 (HTTPS).
  2. The ASP.NET Core Module receives the native request and passes it to the IIS HTTP Server (IISHttpServer). IIS HTTP Server is nothing but the In-Process Server implementation for IIS.
  3. After the IIS HTTP Server Processes the Request, the request is sent to the ASP.NET Core Application Request Processing (Middleware) Pipeline.
  4. The Middleware Pipeline handles the request and passes it on as an HttpContext instance to the application logic. The application logic will get all the requested information from the HttpContext instance and start executing the application logic.
  5. Once the Middleware Pipeline handles the request, it will generate the response, which is sent back to IIS through the IIS HTTP Server.
  6. Finally, IIS sends the response to the user who initiated the request.
What is the ASP.NET Core Module?

The ASP.NET Core Module (ANCM) is a native IIS module that plugs into the IIS Request Processing Pipeline, allowing ASP.NET Core Applications to work with IIS. The ASP.NET Core Module (ANCM) allows running ASP.NET Core Applications with IIS by either:

  1. Hosting an ASP.NET Core Application inside of the IIS worker process (w3wp.exe), called the in-process hosting model.
  2. Forwarding web requests to a backend web server called Kestrel server, which runs the ASP.NET Core Application, called the out-of-process hosting model.

Note: There are trade-offs between each of the hosting models. The in-process hosting model is used by default due to better performance and diagnostics.

What is an IIS HTTP Server?

Internet Information Services, or IIS, is a Microsoft web server that runs on the Windows operating system and is used to exchange static and dynamic web content with internet users. IIS can host, deploy, and manage web applications using technologies such as ASP.NET and ASP.NET Core.

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

Let us understand the InProcess Hosting Model in ASP.NET Core Application with an example. Create a new ASP.NET core application using the ASP.NET Core Empty template. To create a new Empty ASP.NET Core Web Application, open Visual Studio 2022 and click the Create a new project tab, as shown in the image below.

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

Once you click on the Create a new project tab, it will open the Create a new project window. Select the ASP.NET Core Empty project template from this window and click the Next button, as shown in the image below.

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

Once you click on the Next button, it will open the Configure Your New Project window. Here, you must provide the necessary information to create a new project. First, give an appropriate name for your project (FirstCoreWebApplication), set the location where you want to create this project, and the solution name for the ASP.NET Core Web application. And finally, click on the Create button, as shown in the image below.

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

Once you click on the Next button, it will open the Additional Information window. Here, you need to select .NET 6.0 as the Framework. You also need to check the Configure for HTTPS and Do not use top-level statements check boxes, and finally, click on the Create button as shown in the image below.

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

That’s it. Once you click on the Create Button, the project will be created with the Empty template with the following folder and file structure.

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

Main Method of the Program Class in .NET 6 Application:

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

Main Method of the Program Class in .NET 6 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 the Web Host, which will host our application with default preconfigured configurations. The CreateBuilder() method does several things to set the Web host. Some of them are as follows.

  1. Setting up the Web Server 
  2. Hosting the web application
  3. Loading the Host and Application Configuration from various configuration sources 
  4. Configuring logging and many more services

Let us understand what exactly the CreateBuilder() method does to configure and set up the Web Server. From a hosting point of view, an ASP.NET Core Web application can be hosted in two ways, i.e., InProcess Hosting or OutOfProcess Hosting. Here in this article, we will discuss the InProcess Hosting Model; in a later article, we will discuss the OutOfProcess Hosting Model.

How to Configure InProcess Hosting in ASP.NET Core 6?

When we create a new ASP.NET Core Web Application by using any Project Template in .NET 6, by default, the project file is created with InProcess Hosting, which is used for hosting the application in IIS or IIS Express because it will give you better performance than OutOfProcess hosting model.

Method1: 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 then click on the “Edit Project File” option from the context menu, as shown in the image below.

How to Configure InProcess Hosting in ASP.NET Core 6?

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>net6.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 OutOfProcess in the Project Properties

Once you click on the Open debug launch profiles UI button, it will open the Launch Profile window. 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 InProcess as our hosting model in the Project Properties window, you will see that the In Process Hosting Model is selected as the default hosting model. Whatever hosting model you want to use, you can also change it here.

Specify the Hosting Model as OutOfProcess in the Project Properties

What Happens When We Set the Hosting Model as InProcess?

In the case of InProcess Hosting, the application will run inside the IIS Worker Process (i.e., w3wp.exe for IIS and iisexpress.exe for IISExpress). 

From the performance point of view, the InProcess Hosting Model delivers significantly higher request throughput than the OutOfProcess Hosting Model; we will discuss this at the end of this article.

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

How to use InProcess Hosting to Run Application?

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

How to use InProcess Hosting to Run Application?

With this change, Visual Studio will set IIS Express as the Launch Profile, as shown in the below image.

How to use InProcess Hosting to Run Application?

With this change, now run the application, and you will get the following output.

How to use InProcess Hosting to Run Application?

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.

How to use InProcess Hosting to Run Application?

To display the process name in the browser, you need to use System.Diagnostics.Process.GetCurrentProcess().ProcessName 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();
        }
    }
}

When we run the application from Visual Studio, it should display the message in the browser as shown below. This is because, by default, Visual Studio uses IISExpress when we run an application.

What Happens When We Set the Hosting Model as InProcess?

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 then again issue the same request, and you will see that it is showing Microsoft IIS as the server, as shown in the image below.

What Happens When We Set the Hosting Model as InProcess?

What is IIS Express?

The IIS Express is a lightweight, self-contained version of the IIS. It is optimized for Web Application Development. The most important point is that we use IIS Express only in development, not production. In production, we generally use IIS. In a later article, we will discuss deploying an ASP.NET Core Application on IIS.

Why InProcess Hosting Gives Better Performance than the OutOfProcess Hosting Model?

In the case of OutOfProcess Hosting, there are 2 Web Servers

  1. An Internal Web Server (Kestrel) and
  2. One External Web Server (IISNginx, or Apache).

The internal Web Server is called Kestrel; the external web server can be IISNginx, or Apache. With the InProcess Hosting Model, only one web server, i.e., IIS. So, in the case of the InProcess Hosting Model, we do not have the performance penalty for navigating the requests between the internal and external web servers. This is the reason why the InProcess hosting model delivers significantly higher request throughput than the OutOfProcess hosting model.

Why InProcess Hosting Gives Better Performance than the OutOfProcess Hosting Model?

Benefits of InProcess Hosting:
  • Performance: InProcess hosting can provide better performance because there is no need for inter-process communication, serialization, and deserialization of HTTP requests and responses. You generally get better request throughput since there’s no need for proxying requests between the external server (like Kestrel) and IIS. The communication between IIS and the app is in-memory, making it faster.
  • Scalability: Since multiple instances of the application can run within the same process, the memory footprint is reduced, allowing you to host more instances of your application on the same server.
  • Request Processing: In In-Process hosting, the request processing can be more efficient as it doesn’t involve communication between separate processes. This can lead to faster response times.
  • Integrated with IIS features: Since the app runs inside the IIS process, it can take full advantage of IIS features.
  • Simplified Process Model: There’s only one process (the IIS worker process) to consider, which can simplify debugging and troubleshooting.

However, there are some considerations to keep in mind when using InProcess hosting:

  • Isolation: Since the application runs within the same process as the web server, there is less isolation between your application and the server. This might be a concern if you need strong isolation for security reasons.
  • Application Restarts: Any change to the application code or configuration would require the IIS process to be recycled, causing the application to restart. This might lead to a temporary disruption in service.
  • Memory Usage: If your application has memory leaks or consumes a lot of memory, it could potentially impact the stability of the entire IIS process.
  • Less Flexibility: Running directly on Kestrel can offer a more flexible hosting model, especially if you’re not planning to use IIS-specific features.
  • Potential Fault Isolation Issues: Since the app runs within the IIS worker process, if there’s a major issue with the app, it could impact the health of the entire IIS worker process.

Along with the InProcess hosting, we also have OutOfProcess hosting, where the ASP.NET Core application runs in a separate process from the web server. This provides stronger isolation between the application and the server, but it comes with a performance overhead due to the need for inter-process communication.

Next=> So, before understanding the Out Of Process Hosting Model to host our ASP.NET Core Web Application, we first need to understand What is Kestrel Web Server and How Kestrel Web Server works, and 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. Here, in this article, I try to explain the ASP.NET Core InProcess Hosting with Examples. I hope you enjoy this ASP.NET Core InProcess Hosting Model article. 

14 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 *