RECAP: When an ASP.NET core application is executed,
the .NET runtime looks for Main() method which is the entry point for the application.
The Main() method then calls CreateDefaultBuilder() static method of the WebHost class
CreateDefaultBuilder() method performs the following tasks
- Setting up the webserver
- Loading the host and application configuration from various configuration sources and
- Configuring logging
An ASP.NET core application can be hosted InProcess or OutOfProcess.
InProcess hosting in ASP.NET Core
To configure InProcess hosting, add <AspNetCoreHostingModel> element
to the app's project file with a value of InProcess
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
When we create a new ASP.NET Core project using
one of the available project templates, the project defaults to the in-process hosting model for all IIS and IIS
Express scenarios.
In case of InProcess hosting, CreateDefaultBuilder() method
calls UseIIS() method and
host the app inside of the IIS worker process (w3wp.exe or
iisexpress.exe).
- From a performance standpoint, InProcess hosting delivers significantly higher request throughput than OutOfProcess hosting
- In the case of IIS, the process name that executes the app is w3wp and in the case of IIS Express it is iisexpress
- To get the process name executing the app, use System.Diagnostics.Process.GetCurrentProcess().ProcessName
- When we are run the project from Visual Studio it uses IISExpress by default.
- IIS Express is a lightweight, self-contained version of IIS, optimized for application development. We do not use it for production. In production we use IIS.
OutOfProcess hosting in ASP.NET Core
There are 2 ways to configure OutOfProcess hosting
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
2. The default is OutOfProcess hosting. So if we remove the <AspNetCoreHostingModel> element from the project file, OutOfProcess hosting will be used by default.
In case if OutOfProcess hosting, There are 2 web servers - An an internal web server and an external web server.
The internal web server is Kestrel and the external web server can be IIS, Nginx or Apache.
What is Kestrel?
Kestrel is a cross-platform web server for ASP.NET Core. It is supported on all platforms and versions that .NET Core supports. It is included by default as internal server in ASP.NET Core. Kestrel can be used, by itself as an edge server i.e Internet-facing web server that can directly process the incoming HTTP requests from the client. In Kestrel, the process used to host the app is dotnet.exe.
Depending on how you are
running the asp.net core application, the external web server may or may not be
used.
Kestrel is a cross-platform web server that is embedded in your ASP.NET Core
application. With Out of Process Hosting model, Kestrel can be used in one of
the following 2 ways.
Kestrel can be used as the internet-facing
webserver that processes the
incoming HTTP requests directly. In this model, we are not using an external web
server. Only Kestrel is used and it is this server that faces the internet, to
directly handle the incoming HTTP requests. When we run the asp.net core
application using the .NET Core CLI, Kestrel is the only web server that is
used to handle and process the incoming HTTP request.
Kestrel can also be used in combination with a reverse proxy server, such as IIS, Nginx, or Apache.
In Out of Process Hosting, it is the Kestrel server that hosts the application and process the request. The reverse proxy server if used, takes the incoming HTTP request and forwards it to the Kestrel server. Similarly, it takes the response from the Kestrel server and sends it to the client. The name of the process that hosts the application is dotnet.exe.
If Kestrel can be used as a web server, why do we need a reverse proxy server?
With Out of Process Hosting, using a reverse proxy server is a good choice as it provides an additional layer of configuration and security. It might integrate better with the existing infrastructure. It can also be used for load balancing.
Can we run an asp.net core application without the built-in kestrel web server?
The answer is YES. If we use the InProcess hosting model, the application is hosted inside of the IIS worker process (w3wp.exe or iisexpress.exe). Kestrel is not used with the InProcess hosting model
Comments
Post a Comment