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. In ASP.NET Core, hosting refers to the process of running your web application and making it accessible over the internet or intranet.

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, how the requests are processed, and how the responses are returned to the 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 the IIS Worker Process, i.e., w3wp.exe, and IIS is the only Web Server that will handle the Incoming HTTP Request.

The In-Process Hosting Model provides better performance than the Out-of-Process model. With In-Process Hosting, the ASP.NET Core application runs as part of the IIS worker process. The application code and the IIS worker process share the same memory space, eliminating communication overhead between the web server and the application process.

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

When a request comes to IIS, it is directly processed by the ASP.NET Core application in the same process. There’s no proxy or intermediary involved. Please look at the following diagram to better understand how the InProcess Hosting Model works. Here, we only use one server, IIS, to handle client requests.

How InProcess Hosting Model Work in ASP.NET Core?

The InProcess Hosting Model works as follows:

  1. The client made a request to IIS on the website’s configured port through the Internet over the 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). The IIS HTTP server is nothing but the implementation of the in-process server 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 integrates ASP.NET Core applications with the Internet Information Services (IIS) web server. It serves as the interface between IIS and the ASP.NET Core application.

Example of Understanding the InProcess Hosting Model in ASP.NET Core Application.

Let’s understand the InProcess Hosting Model in an ASP.NET Core Web Application with an example. Create a new ASP.NET Core Web application using the ASP.NET Core Empty Project 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, the Create a new project window will open. In this window, select the ASP.NET Core Empty project template 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

The Additional Information window will open once you click on the Next button. Here, you need to select .NET 6.0 as the Framework. You must also check the Configure for HTTPS and do not use top-level statements check boxes. Finally, click 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 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 in ASP.NET Core 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 internally 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: in-Process Hosting or out-of-Process Hosting. 

How do you Configure InProcess Hosting in ASP.NET Core?

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

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

The Launch Profile window will open once you click on the Open debug launch profiles UI button. 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 IIS. Similarly, if it is IIS Express, the process name will be iisexpress.

How do you use InProcess Hosting to run an application?

To use the InProcess hosting model in Visual Studio, we first 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?

Using the browser developer tool, you can verify whether it is using a Microsoft IIS server. Run the application and open the browser developer tool by pressing the F12 key. Then, go to the Network tab and again issue the same request. 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?

IIS Express is a lightweight, self-contained version of IIS that 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 does InProcess Hosting give 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?

What 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, 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. In this article, I try to explain 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. blank

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

      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 *