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
- Supports Http/2
- 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 .NetCore 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 Kestrel server directly as user request facing server or we can deploy the app into IIS which will act as a proxy server and sends requests to 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.
Using Proxy Server
Due to limitations of Kestrel server, we can not use this in all the apps so 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 like one is IIS and another is Kestrel.
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 as 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 Kestrel server to host app 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.
Run the application on *IISExpress * server then open the browsers network tab and check for the first call. Under the server section, you will able to see its showing Microsoft IIS.
Now open the command prompt and run the same application using dotnet CLI bu using command dotnet run. Now it will host app on http://localhost:5000
Browse the URL and open network tab and see the server attribute as Kestrel
.
How to change hosting model manually ?
You may have one question in your mind like where are these type defines?
There are two ways to define the models:
- .csproj file----From your Visual Studio before Publish
- web.config file---From your website root folder after Publish
In .csproj file
Open the .csproj file and add the below property to apply the proper hosting model.
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>
So the value <AspNetCoreHostingModel> property is case insensitive and if we don't define this property then it by deafults 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:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\DotNetCoreApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</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".
To troubleshoot more issue about host asp.net core applications, you can refer to Microsoft guide here: