Hosting Models in ASP.NET Core 3.1 Applications

Introduction 
 
In this article, we will be discussing different hosting models provided by ASP.NET Core 3.1. Once you successfully developed your web application, what should be the next step you have to do? The answer is Hosting. We have to host our application to the server so that other people can access it. The process of deploying/installing an application into the server is called "Hosting".
 
Whenever you create an ASP.NET Core application, by default it contains an internal server provided by a .NET Core that is called Kestrel. Due to this server, we can run ASP.NET Core apps on any platform like Windows, Mac or Linux. Before getting into the details about hosting models, let's first see what is the Kestrel server.

What is the Kestrel Server?

According to the ASP.NET core docs:
 
Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the webserver that's included by default in ASP.NET Core project templates.
 
Kestrel is based on the libuv library, the same library which is used by Node.
 
Some features of Kestrel:
  • It supports SSL
  • It supports SSL
  • lightweight
  • cross-platform

Hosting Models in ASP.NET Core

 
There are 2 types of hosting models in ASP.NET Core i.e In-process Hosting and Out-of-process Hosting. Before ASP.Net Core 2.2 we have only one hosting model which is Out-of-process but after due to the performance we have In Process Hosting Model in 2.2+ versions.
 

Out-of-process Hosting Model 

 
In Out-of-process hosting models, we can either use the Kestrel server directly as a user request facing server or we can deploy the app into IIS which will act as a proxy server and sends requests to the internal Kestrel server. In this type of hosting model we have two options:
 

Using Kestrel 

 
So in this type Kestrel itself acts as edge server which directly server user requests. It means that we can only use the Kestrel server for our application.
 
Hosting Models In ASP.NET Core 3.1 Applications
 
Using a Proxy Server
 
Due to limitations of the Kestrel server, we can not use this in all the apps. In such cases, we have to use powerful servers like IIS, NGINX or Apache. So, in that case, this server acts as a reserve proxy server which redirects every request to the internal Kestrel sever where our app is running. Here, two servers are running. One is IIS and another is Kestrel.
 
Hosting Models In ASP.NET Core 3.1 Applications
 
This model is a default model for all the applications implemented before .NET Core 2.2. But there are some of the limitations of using this type such as performance slowness.
 
In-process Hosting Model
 
After the release of .NET Core 2.2, it introduced a new type of hosting which is called In-process hosting. In this type, only one server is used for hosting like IIS, Nginx or Linux. It means that the App is directly hosted inside of IIS. No Kestrel server is being used. IIS HTTP Server (IISHttpServer) is used instead of the Kestrel server to host apps in IIS directly. ASP.NET Core 3.1 onwards **In-process** hosting model is used as a default model whenever you create a new application using an existing template.
 
Hosting Models In ASP.NET Core 3.1 Applications
 
We have discussed different types of hosting models. Now let's see how to check which hosting model is being used. To do this, we have to first create a new ASP.NET core application. If you are new to ASP.NET, you can check the steps here.
 
Run the application on the IISExpress server, then open the browsers network tab and check for the first call. Under the server section, you will able to see it showing Microsoft IIS. 
 
Hosting Models In ASP.NET Core 3.1 Applications
 
Stop the app and open the command prompt and run the same application using dotnet CLI bu using the command dotnet run. Now it will host app on http://localhost:5000.
 
Hosting Models In ASP.NET Core 3.1 Applications
 
Browse the URL and open the network tab to see the server attribute as Kestrel.
 
Hosting Models In ASP.NET Core 3.1 Applications
 
Where are these types defined? There are two ways to define the models:
  • In.csproj file
  • In web.config file 
In .csproj file
 
Open the .csproj file and add the below property to apply the proper hosting model.
  1. <PropertyGroup>  
  2.     <TargetFramework>netcoreapp3.1</TargetFramework>  
  3.     <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>  
  4. </PropertyGroup>  
So the value <AspNetCoreHostingModel> property is case insensitive and if we don't define this property then it by default consider as In-process hosting model.
 
In web.config 
 
In ASP.NET Core apps we don't have `web.config` so first, we have to publish the app and in the published folder you can see the `web.config` the file generated by ASP.NET Core. Now published the app you have created earlier and open the config file. It looks like this:
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <configuration>    
  3.   <location path="." inheritInChildApplications="false">    
  4.     <system.webServer>    
  5.       <handlers>    
  6.         <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />    
  7.       </handlers>    
  8.       <aspNetCore processPath="dotnet" arguments=".\DotNetCoreApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />    
  9.     </system.webServer>    
  10.   </location>    
  11. </configuration>    
As I mentioned earlier from ASP.NET Core 3.1, the **In-process** hosting model is the default model. So if you want to change it to other i.e. Out -of-process then you just need to change `hostingModel="OutOfProcess" `.

Conclusion 

In this article, I have explained the different types of hosting models provided by ASP.NET Core(i.e In-process & Out-of-process). I really hope that you enjoyed this article, and please do not hesitate to send me your thoughts or comments about what could I have done better.
 
You can follow me on twitter @sumitkharche01.
 
Happy Coding!