Part - 4 ASP - NET Core InProcess Hosting

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

ASP.

NET Core InProcess Hosting


In ASP.NET Core, there are two types of Hosting Model, one is InProcess and another
is OutOfProcess. Let us first discuss InProcess Hosting Model in detail.

What is InProcess Hosting Model in ASP.NET Core?


In the case of the InProcess Hosting Model, the ASP.NET Core Application is hosted inside of the IIS
Worker Process i.e. w3wp.exe and IIS is the only server going to handle the request.

How InProcess Hosting Model Work in ASP.NET Core?


For a better understanding of how the InProcess Hosting Model works, please have a look at the
following diagram. Here, you can see, we are only using one server i.e. IIS which is going to handle the
request coming from the clients.

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 IIS HTTP Server
(IISHttpServer).
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 and that
response is sent back to IIS through IIS HTTP Server.
6. And, finally, IIS sends the response back to the user who initiated the request.

What is ASP.NET Core Module?


The ASP.NET Core Module (ANCM) is a native IIS module that plugs into the IIS Request Processing
Pipeline which allows 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. By default, the in-process hosting
model is used due to better performance and diagnostics.

What is IIS HTTP Server?


Internet Information Services, also known as 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
be used to host, deploy, and manage web applications using technologies such as ASP.NET and
ASP.NET Core.

Trainer: Pranaya Kumar Rout Website: https://dotnettutorials.net/


Example to Understand InProcess Hosting Model in ASP.NET Core Application.
Let us understand 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, First, open Visual Studio 2022 and then click on the Create a new
project tab as shown in the below image.

Once you click on the Create a new project box, it will open the “Create a new project” window. This
window includes different .NET 6 application templates. Here we will create a simple Web application
from scratch, so select the ASP.NET Core Empty Project template and then click on the Next button
as shown in the below image.

Once you click on the Next button, it will open the following Configure Your New Project window.
Here, you need to 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 Next button as shown in the image below.

Trainer: Pranaya Kumar Rout Website: https://dotnettutorials.net/


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 below image.

Once you click on the Create button, it will create a new ASP.NET Core Web Application in Visual Studio
2022 using .NET 6 with the following file and folder structure.

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


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

Trainer: Pranaya Kumar Rout Website: https://dotnettutorials.net/


When we execute an ASP.NET Core Web Application then 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 call the static CreateBuilder() method.

What are the Tasks Performed by CreateBuilder() Method in ASP.NET Core?


The CreateBuilder() method sets the Web Host which will host our application with default preconfigured
configurations. As part of setting the Web host, the CreateBuilder() method does several things. 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. Let us first discuss the InProcess Hosting Model and
then, 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 out of process 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, just add the <AspNetCoreHostingModel> element to the application project file with a
value of InProcess. To do so, just 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 below code.

Trainer: Pranaya Kumar Rout Website: https://dotnettutorials.net/


Once you open the Application Project file then modify it as shown below. As you can see, here 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. From this window, select the Debug tab
and click on the Open debug launch profiles UI button as shown in the below image.

Once you click on the Open debug launch profiles UI button, then it will open the Launch Profile window.
From this window select IIS Express and scroll down and somewhere you will find the Hosting Model

Trainer: Pranaya Kumar Rout Website: https://dotnettutorials.net/


drop-down list with the following values. As we have already set InProcess as our hosting model in the
Project Properties window, then you will see that here 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. Why we will discuss this latter part of this session.

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

How to use InProcess Hosting to Run Application?


In order to use the InProcess Hosting Model in Visual Studio, first we need to set the launch profile as
IIS Express as shown in the below image.

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

Trainer: Pranaya Kumar Rout Website: https://dotnettutorials.net/


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 below image.

To display process name in 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();
}
}
}

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

Trainer: Pranaya Kumar Rout Website: https://dotnettutorials.net/


You can also verify whether it is using a Microsoft IIS server or not by using the browser developer tool.
Run the application and open the browser developer tool by pressing the F12 key and 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 below image.

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 that you need to remember is we use IIS Express only in
development, not in production. In production we need to use IIS. In our upcoming session, we will
discuss how to deploy 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 (IIS, Nginx, or Apache).

The internal Web Server is called Kestrel and the external web server can
be IIS, Nginx, or Apache. With the InProcess Hosting Model, there is only one web server i.e. IIS. So,

Trainer: Pranaya Kumar Rout Website: https://dotnettutorials.net/


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.

Next=> So, before understanding the Out of Process Hosting Model to host our ASP.NET Core Web
Application, first we 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.

Trainer: Pranaya Kumar Rout Website: https://dotnettutorials.net/

You might also like