ASP.NET Core Out of Process Hosting
ASP.NET Core Out Of Process Hosting
ASP.NET Core Out of Process Hosting
In this article, I am going to discuss ASP.NET Core OutOfProcess Hosting Model in detail. I strongly recommended you to read ASP.NET Core InProcess Hosting and Kestrel Web Server in ASP.NET Core Application 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 work 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?
What happens when we run the ASP.NET Core application using .NET Core CLI?
Can we run an asp.net core application without using the built-in kestrel web server?
Before understanding the OutOfProcess hosting, let us first have a look at the InProcess hosting model and understand how it works.
ASP.NET Core InProcess Hosting
Let first have a look at InProcess Hosting before proceeding to Out of Process Hosting. As we already discussed, by default Visual Studio uses InProcess Hosting. You can verify this using the Debug of your project properties. 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. In this window, select the Debug tab and have a look at the Hosting Model value as shown in the below image.
Even though if you want then you can also configure the same (i.e. InProcess hosting) in the Application Project file by setting the value to InProcess of the <AspNetCoreHostingModel> element as shown in the below image.
In ASP.NET Core, with InProcess Hosting Model our application is going to be hosted in the IIS worker process. 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.
Way1: In this case, you just need to add the <AspNetCoreHostingModel> element to the applications project file with a value of OutOfProcess as shown below.
Way2: Specify the Hosting Model as OutOfProcess in the Project Properties
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. In this window, select the Debug tab and then select the Hosting Model value as OutOfProcess as shown in the below image.
What is Out of Process Hosting in ASP.NET Core?
In the case of the ASP.NET Core OutOfProcess Hosting Model, there are two web servers.An internal webserver which is the Kestrel web ServerAnd 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 the 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 that is already embedded with your ASP.NET Core application. So, if you are using 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.
Way1:
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 the command prompt and run the application as shown in the below image. As my First, you need to change the directory to the folder which contains your asp.net core application. My project is present in the “D:\Projects\Core\FirstCoreWebApplication\FirstCoreWebApplication” folder so I change the current directory to my project file and then use the dotnet run command.
Once you type dotnet run command and press the enter key, it will build, host, and run the application as shown in the below image.
Now open the browser window and navigate to the following URL which is shown in the command prompt. In my case, the following is the.
http://localhost:5000
And here you will see the worker process name as your application name as shown below
So, in this case, Kestrel is the only server that will handle and process the incoming HTTP Request. The following code of the Startup class displays the worker process name.
namespace FirstCoreWebApplication
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Worker Process Name : " + System.Diagnostics.Process.GetCurrentProcess().ProcessName);
});
});
}
}
}
Way2:
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 should come 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.
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
As we have configured the 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 your project name. 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.
What happens when we run the ASP.NET Core application using .NET Core CLI?
When we are running our application using.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 the AspNetCoreHostingModel element is going to be ignored.
The .NET Core CLI always uses OutOfProcess Hosting Model and Kestrel is the webserver that 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.
Leave a Comment