Back to: ASP.NET Core Tutorials For Beginners and Professionals
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.
- ASP.NET Core Hosting Models
- What is the InProcess Hosting Model in ASP.NET Core?
- How Does the InProcess Hosting Model Work in ASP.NET Core?
- Example to Understand InProcess Hosting Model in ASP.NET Core Application
- What are the Tasks Performed by the CreateBuilder() Method in ASP.NET Core?
- How to Configure InProcess Hosting in ASP.NET Core?
- What Happens When We Set the Hosting Model as InProcess?
- How to use InProcess Hosting to Run Application?
- What is IIS Express?
- 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.
The InProcess Hosting Model works as follows:
- 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).
- 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.
- After the IIS HTTP Server Processes the Request, the request is sent to the ASP.NET Core Application Request Processing (Middleware) Pipeline.
- 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.
- Once the Middleware Pipeline handles the request, it will generate the response, which is sent back to IIS through the IIS HTTP Server.
- 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:
- Hosting an ASP.NET Core Application inside of the IIS worker process (w3wp.exe), called the in-process hosting model.
- 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.
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.
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.
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.
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.
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.
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.
- Setting up the Web Server
- Hosting the web application
- Loading the Host and Application Configuration from various configuration sources
- 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.
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.
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.
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 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.
With this change, Visual Studio will set IIS Express as the Launch Profile, as shown in the below image.
With this change, now run the application, and you will get the following output.
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.
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.
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 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
- An Internal Web Server (Kestrel) and
- One External Web Server (IIS, Nginx, or Apache).
The internal Web Server is called Kestrel; the external web server can be IIS, Nginx, 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.
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.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.
It’s very simple and effective
very good
The AspNetCoreHostingModel setting is looked up in process of application deploy or runtime? maybe deploy
Thank you. Very useful!
what a tutorial
Sections in this article is not grouped and follow order, so it is quite hard to see the whole picture of this topic
Can you please elaborate on what you are not getting? I hope we have covered everything.
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?
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? element to the application project file with a value of InProcess.
To configure the InProcess hosting for ASP.NET Core Web application there is only one simple setting, just add the
Really impressive points.
I totally appreciate the work put into making these docs. Thank you. Very well detailed.
great tutorial. Thanks.
Great….
Thanks….
Simple and easy to learn new things thank you