ASP.NET Core Out Of Process Hosting in Detail
In this article, I am going to discuss ASP.NET Core OutOfProcess Hosting Model in detail. I strongly recommended you to read the following two articles before proceeding to this article.
As part of this article, we are going to discuss the following pointers in detail.
- How to Configure OutOfProcess Hosting in ASP.NET Core?
- What is ASP.NET OutOfProcess Hosting?
- How does the OutOfProcess Hosting works in ASP.NET Core?
- Can we run an asp.net core application without using the built-in kestrel web server?
- If Kestrel can be used by itself as a web server which can directly handle and process the incoming HTTP Request, then why do we need a reverse proxy server?
Before understanding the OutOfProcess hosting, let us first have a look at the InProcess hosting model.
ASP.NET Core InProcess Hosting
Let first have a look on in Process Hosting before proceeding to Out Of Process Hosting
As we already discussed, to configure the InProcess hosting in ASP.NET Core application, you need to add the <AspNetCoreHostingModel> element to the application’s project file with a value of InProcess as shown below
In ASP.NET Core, with InProcess Hosting Model our application is going to be hosted in the IIS worker process (i.e. w3wp.exe in case of IIS server or iisexpress.exe if the hosting server is IISExpress). The most important point that you need to remember is we have only one web server i.e. IIS Server in case of InProcess hosting which is going to host our application as shown in the below image.
How to Configure the OutofProcess Hosting in ASP.NET Core Application?
We can configure the Out Of Process Hosting in two ways in ASP.NET Core.
In this case, you just need to add the <AspNetCoreHostingModel> element to the applications project file with a value of OutOfProcess as shown below.
The default hosting in ASP.NET Core is OutOfProcess Hosting. That means if you remove the <AspNetCoreHostingModel> element from the application’s project file, then by default OutOfProcess hosting will be used.
What is Out of Process Hosting in ASP.NET Core?
In the case of ASP.NET Core OutOfProcess Hosting Model, there are two web servers.
- An internal webserver which is the Kestrel web Server
- And an external web server which can be IIS, Apache, and Nginx.
The most important point that you need to keep in mind is depending on how you are running your application with OutOfProcess hosting model, the external web server may or may not be used.
As we already discussed that the Kestrel web server is a cross-platform web server which is already embedded with your ASP.NET Core application. So if you are using the Out of Process Hosting model for your asp.net core application, then the Kestrel web server can be used in one of the following ways.
We can use the Kestrel Web Server as the internet-facing web server which will directly process the incoming HTTP requests. In this scenario, only the Kestrel Server is used and the other one i.e. external web server is not going to be used. So when we run the application using the .NET core CLI then Kestrel is the only web server that is going to be used to handle and process the incoming HTTP request as shown in the below image.
To confirm this, open command prompt and run the application as shown in the below image
Now open the browser window and navigate to the following URL
And here you will see the worker process name as dotnet as shown below
So, in this case, Kestrel is the only server which will handle and process the incoming HTTP Request. The following code of the Startup class displays the worker process name.
The Kestrel Web Server can also be used with the combination of a reverse proxy server such as IIS, Apache or Nginx. Now the question that comes to your mind is
If Kestrel can be used by itself as a web server which 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 and security which is not available with the Kestrel Server. It also maintains the load balancing. So it is a good choice to use Kestrel Server along with a reverse proxy server.
So when we use Kestrel Server along with a reverse proxy server, then the reverse proxy server will receive the incoming HTTP requests from the client and then forwards that request to the Kestrel server for processing. Once the Kestrel Server process that request, then it sends the response back to the reverse proxy server which then sends the response back to the requested client over the internet as shown in the below image.
In our upcoming articles, we will discuss how to deploy an ASP.NET Core application to IIS and how we can use IIS as a reverse proxy server.
When we run the application directly from Visual Studio, then by default Visual Studio uses IIS Express. Now change the AspNetCoreHostingModel element value as shown below in the application’s project file.
As we have configured Out of Process hosting model, now the IIS Express acts as a reverse proxy server and Kestrel acts as the internal webserver.
Now, the 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 and sends the response back to the IIS Express which in turn sends the response back to the client i.e. to the browser.
Now run the application, and you will see the worker process as dotnet. So when you are using Out of Process Hosting model, then the Kestrel Web Server is going to hosts the application and process the request irrespective of whether you are using a reverse proxy server or not.
One more thing that you need to keep in mind is, when you are running your application using the .NET Core CLI, then by default, it ignores the hosting setting that you specified in the application’s project file i.e. csproj file. So, in that case, the value of AspNetCoreHostingModel element is going to be ignored.
The .NET Core CLI always uses OutOfProcess Hosting Model and Kestrel is the webserver which will host the ASP.NET Core application and also handles the HTTP requests.
Can we run an asp.net core application without using the built-in kestrel web server?
YES. When we use the InProcess Hosting model, then the application is hosted inside the IIS worker process i.e. w3wp.exe in the case of IIS and iisexpress.exe in the case of IIS Express. That means the Kestrel Web Server is not used with the InProcess hosting model.
In the next article, we will discuss the launchSettings.json file in ASP.NET Core application. Here, in this article, I try to explain the ASP.NET Core OutOfProcess Hosting model in detail. I hope this article will help you to understand the OutOfProcess Hosting model in ASP.NET Core Application.
No HTML was returned.