Kestrel Web Server in ASP.
NET Core Application
I will discuss the Kestrel Web Server in ASP.NET Core Application with Examples.
What is Kestrel Web Server?
As we already discussed, ASP.NET Core is a Cross-Platform framework. It supports developing and running
applications on operating systems such as Windows, Linux, or MacOS. The Kestrel is the Cross-Platform Web
Server for the ASP.NET Core Web Application. This Server supports all the platforms that the ASP.NET Core
Supports.
Kestrel is built for high performance and can handle a large number of concurrent requests, making it
suitable for high-traffic websites and applications.
Kestrel is cross-platform, meaning it runs on Windows, Linux, and macOS.
By default, it is included as the Internal Web Server in the ASP.NET Core application. But if you want, you
can also use it as an Internet-facing web Server, i.e., an External Web Server. In that case, the Kestrel
Web Server is used as an Edge Server, i.e., the Internet-facing web Server, which will directly process the
incoming HTTP request from the client.
What is the Process Name for the Kestrel Web Server?
In ASP.NET Core, when using the Kestrel web server, the application code executes within the Kestrel server’s
worker process. When we run an ASP.NET Core application using Kestrel, the process name is the name of the
application’s executable file. For example, if we have an ASP.NET Core application named
FirstCoreWebApplication and publish it as a self-contained application, creating an executable file named
FirstCoreWebApplication.exe, the process name will be FirstCoreWebApplication.
How Do We Run Applications Using Kestrel Web Server in ASP.NET Core?
Before using the Kestrel Web Server to host and run our ASP.NET Core application, let us first open
the launchSettings.json file, which is present inside the Properties folder. Once you open the launchSettings.json
file, you will find the following code by default.
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:16145",
"sslPort": 44335
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5051",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7084;http://localhost:5051",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Note: In our example, for IIS Express, the port number is 16145 for HTTP and 44335 for HTTP, and the worker
process will be iisexpress. On the other hand, for the Kestrel Server (command name Project), the port number is
5051 for HTTP and 7084 for HTTPS, and the worker process name will be FirstCoreWebApplication (it is nothing
but assembly name, or you can say the project name).
In our upcoming session, we will discuss launchSettings.json in detail. But for now, have a look at the Lunch
Profiles section. You can see four options here, as shown in the image below.
IIS Express
It Uses IIS Express as the web server. By default, the application runs in-process with IIS Express (i.e., the
ASP.NET Core app runs within the IIS Express worker process itself).
It can support HTTP and HTTPS connections with the URLs specified in the launchSettings.json. This
makes it useful for simulating the IIS hosting environment during development.
Kestrel (HTTP, HTTPS)
It uses Kestrel, the cross-platform web server developed for ASP.NET Core. The application runs directly
in the Kestrel Web Server Worker process.
The launch profile can specify whether Kestrel listens on HTTP or HTTPS, and you can define custom
ports and URLs in the launchSettings.json. It is ideal for testing the application in an environment close to
what would be used in production for Linux deployments or when not using IIS as a reverse proxy.
WSL (Windows Subsystem for Linux)
It uses Kestrel. Runs the application within a Linux-like environment provided by WSL. The application
executes as if running on a Linux machine, using the Linux environment’s system libraries and paths.
It is useful for developers who want to ensure their application works well in a Linux environment,
possibly in preparation for deployment on Linux servers.
Example to Understand Kestrel Web Server in ASP.NET Core:
To display the process name which executes our application code in the browser we need to
use System.Diagnostics.Process.GetCurrentProcess().ProcessName is within the Main method of the Program
class. So, please modify 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();
}
}
}
Running the Application:
If we run the application using the IIS Express Profile, it will use the URL and Port Number mentioned in the IIS
Settings of your launchSettings.json file. To prove this, run the application using IIS Express, and see the output
below.
Running the Application using Kestrel Server:
In order to use the Kestrel Web Server to host and run our application in Visual Studio, we need to run the
application using either HTTP or HTTPS profile. Let’s run the application using the https profile. Once you run the
application using the HTTPS profile, you need to observe two things.
First Thing:
First, it will launch the command prompt and host the application using the Kestrel Web Server, as shown below.
Here, you need to focus on the URL and Port Number, which should be the URL and Port Number mentioned in
your HTTPS profile of the launchSettings.json file.
Second Thing:
Secondly, it opens the default browser and listens to that URL and Port Number, which uses the HTTPS protocol,
as shown in the image below.
You can also verify whether it is using the Kestrel Web 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
refresh the page. You should see that it is showing Kestrel as the server, as shown in the image below.
Note: In our example, Kestrel is the only Web Server hosting our ASP.NET Core Web Application and Handling the
incoming HTTP Requests. We don’t need to install Kestrel Web Server additionally. It is included by default in
ASP.NET Core, and this Kestrel Web Server makes ASP.NET Core cross-platform.
How Do We Run ASP.NET Core Web Application using .NET Core CLI?
We can also run the ASP.NET Core Web Application from the command prompt using the .NET Core CLI
commands. The CLI stands for Command Line Interface. When we run an ASP.NET Core application using the .NET
Core CLI, the .NET Core Runtime uses Kestrel as the only web server to host the application and handle the
incoming HTTP requests. We will discuss the .NET Core CLI Command in detail in our upcoming session. Now, let
us see how to run a dot net core application using .NET Core CLI Command. To start with .NET Core CLI, open the
command prompt, type dotnet, and press the enter button, as shown below.
Then, to see all the CLI commands we can use to create, build, and run the Dot Net Core application, we need to
use the dotnet help command. So, open the command prompt, type dotnet help, and press enter, as shown
below.
Using the CLI (Above Commands)
You can create a new project using the new command, build the project using the build command, run the
application using the run command, or publish the project using the publish command. Using the CLI, you can
also restore the dependencies and tools required for a .net core project.
Running ASP.NET Core application using .NET Core CLI
Let’s see how to run an ASP.NET Core Web Application using the .NET Core CLI command. To do so, please follow
the below steps.
First, open the Windows Command Prompt. To do so, open the run window, type cmd, and click the enter button
to open the command prompt. Then, you need to change the directory to the folder that contains your ASP.NET
Core Web Application. My project is in the “D:\Projects\FirstCoreWebApplication\FirstCoreWebApplication”
folder, so I changed the current directory to my project file using the following command.
Once you change the directory to your project folder, execute the “dotnet run” command, as shown in the image
below.
Once you type the dotnet run command and press the enter key, the .NET Core CLI builds and runs the
application. It also shows the URL, which you can use to access your application, as shown in the image below.
What Hosting Model will be used when we run the Application using Dot Net CLI Command?
When we run an ASP.NET Core application using the dotnet run CLI command, it does not use either the
InProcess or OutOfProcess hosting models, which are specific to when the application is hosted behind IIS
(Internet Information Services). Instead, the application runs directly on Kestrel, which serves as its web server.
Next=> Once we understand the In-Process Hosting Model and Kestrel Web Server, we need to understand the
Out-of-Process Hosting Model using an ASP.NET Core Web Application.
ASP.NET Core Out of Process Hosting
In this article, I will discuss the ASP.NET Core Out-of-Process Hosting Model with Examples.
1. What is Out of Process Hosting in ASP.NET Core?
2. How Do We Configure OutofProcess Hosting in ASP.NET Core Application?
3. How Do We Use Out-of-Process Hosting in ASP.NET Core?
4. Kestrel as Internet-Facing Web Server
5. What is the Role of the IIS Worker Process?
6. What is the Role of the Kestrel Web Server Process?
7. When and Which Server is Used to Host and Process the Application?
What is Out of Process Hosting in ASP.NET Core?
The ASP.NET Core Out-of-Process hosting model hosts the ASP.NET Core application in a worker process separate
from the web server worker process that handles incoming HTTP requests. This is different from the In-Process
hosting model, where the ASP.NET Core application runs inside the same process as the web server.
The out-of-process hosting model uses Kestrel as the webserver to run the application code, with another web
server (such as IIS, Nginx, or Apache) acting as a reverse proxy, which will receive the incoming request from the
client and then forward the requests to the Kestrel server. So, in the case of the ASP.NET Core Out-of-Process
Hosting Model, there are two web servers.
An internal web server, i.e., the Kestrel web server.
An external web server can be IIS, Apache, Nginx, etc.
How Do We Configure 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
In this case, we 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>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>
</Project>
Method 2: Specify the Hosting Model as OutOfProcess using GUI
To do so, right-click on your project in the Solution Explorer and click on the Properties option from the context
menu. This 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 image below.
Once you click on the Open debug launch profiles UI button, the Launch Profile window will open. 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 OutOfProcess as our hosting model in the Project Properties window,
you will see that the Out of Process Hosting Model has been selected as the default hosting model.
How Do We Use Out-of-Process Hosting in ASP.NET Core?
The point that you need to remember is that if we use the HTTP or HTTPS profiles to run our application, neither
the InProcess nor the OutOfProcess hosting model will come into the picture. In that case, Kestrel is the only web
server host and runs the application. The InProcess and OutOfProcess hosting models will come into the picture
when we launch the application using IIS.
With the In-Process Hosting Model, IIS is the only web server that will host the application and handle the
incoming HTTP requests. On the other hand, with the Out-of-Process Hosting Model, IIS will be the External Web
Server, which will receive the incoming HTTP requests from the client, and Kestrel will be the Internal Web
Server, which will process the request by executing the application code. In this case, IIS will forward the
incoming request to the Kestrel Web Server.
Kestrel as Internet-Facing Web Server (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, the Kestrel Server is the only web server that is going to host the application and
handle the incoming HTTP Requests by executing the application. The external web server (i.e., IIS, Apache, and
Nginx) will not be used.
So, when we run the application using an HTTP or HTTPS profile, Kestrel is the only Web Server that will handle
the incoming HTTP request, as shown in the image below. Here, dotnet.exe is nothing but the worker process
name for the Kestrel Web Server; for example, if the project name is FirstCoreWebApplication, it will be
FirstCoreWebApplication.exe.
To confirm this, run the application using either the HTTP or HTTPS profiles and verify the worker process name,
as shown in the image below. Here, it shows FirstCoreWebApplication, which means Kestrel Server is executing
our application code.
Now, you can also verify the server that processes the request using the browser developer tool, as shown in the
image below. Kestrel is the Web Server that handles the request.
This proves that when we use the HTTP or HTTPS profile to launch our application, Kestrel is the only Web Server
that hosts our application and executes the application code. In this case, Kestrel works as an Internet-facing
Web Server.
Kestrel as Internal Web Server
Kestrel can also be used as an Internal Web Server. That means it can be used with a Reverse Proxy Server
(Internet Facing Server) such as IIS, Apache, or Nginx. Now, the question that should come to your mind is, if
Kestrel can be used by itself as a Web Server that can directly handle and process the incoming HTTP Request,
then why do we need a Reverse Proxy Server?
This is because the Reverse Proxy Server provides an additional layer of configuration, load balancing, URL
Rewriting, and security, which is not available with the Kestrel Server. The Kestrel Server’s main job is to execute
the application code in Cross-Platform. So, it is a good choice to use the Kestrel Web Server along with a Reverse
Proxy Server such as IIS, Apache, or Nginx.
So, when we use the 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 with its own worker process,
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.
The flow of the ASP.NET Core Out of Process Hosting Model is as follows:
A client sends a request to IIS.
IIS uses the ASP.NET Core Module to forward the request to Kestrel.
Kestrel runs the application code to handle the request.
The application processes the request and generates a response.
Kestrel sends the response back to IIS.
IIS will forward the response to the client.
What is the Role of the IIS Worker Process?
The IIS Worker Process handles the initial part of the request lifecycle. This means it provides basic web server
functionalities such as SSL, authentication, and serving static files (if configured to do so). It can also manage URL
rewriting and other web server-level configuration, load balancing, and security tasks.
After processing the initial request and applying the necessary server-level configurations or security measures,
the request is forwarded to the Kestrel server via the ASP.NET Core Module.
What is the Role of the Kestrel Web Server Process?
Kestrel Server’s worker process executes the application code, including everything from processing requests to
handling routes to executing middleware components to running controllers and other application logic.
After the application code processes, Kestrel generates the HTTP response. This includes setting response
headers and status codes and forming the response body based on the application logic’s output. Once the
response is prepared, Kestrel sends it back to the IIS Worker Process through the ASP.NET Core Module. IIS then
forwards this response to the client.
Kestrel as an Internal Web Server:
Now, set the hosting model to OutOfProcess in the project properties file and run the application using the IIS
Express profile. As shown in the image below, you will see the worker process as your project name. You can also
verify in the taskbar that the IIS Server is running.
With the Out-of-Process Hosting Model, when we use IIS Express as the launch profile to run the application, IIS
Express acts as the Reverse Proxy Server (Internet Facing Server), and Kestrel acts as the internal web server.
Now, IIS Express receives the incoming HTTP request and then forwards it to the Kestrel Web Server for
processing. The Kestrel Web Server processes the request, generates the response, and sends it back to IIS
Express, which in turn sends the response back to the client, i.e., to the browser.
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.