Kestrel

Kestrel is a cross-platform Web server for ASP.NET Core that is included by default in the ASP.NET Core project template.

Kestrel supports the following options:

  • HTTPS
  • Opaque upgrade for enabling WebSocket
  • Unix sockets for Nginx high performance
  • HTTP/2 (except macOS†)

Kestrel can be used alone or in conjunction with a reverse proxy server such as Internet Information Services (IIS), Nginx, or Apache. The reverse proxy server receives HTTP requests from the network and forwards them to Kestrel.

Kestrel as edge (Internet oriented) Web server:

Kestrel is used for reverse proxy configuration:

The instance

For a more straightforward look, here we create a new ASP.NET Core Web application using VS Code and using Kestrel as the server.

First you need to install it on your computer. NET Core environment. You can check out the official website here. In this example, it is. NET Core 3.1.

  • Create an APS.NET Core Web application

Open a command window in a directory and type the command:

dotnet new webapp -o myapp
Copy the code

This creates a new project using the default template. If it is the first installation. NET Core, also requires a trust certificate:

dotnet dev-certs https --trust
Copy the code

At this point, the project can run directly:

cd myapp
dotnet watch run
Copy the code

At this point, the built-in Kestrel server runs directly. Let’s modify the code to look at the runtime process and modify Pages/ index.cshtml as follows:

@page

@using System.Diagnostics
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>
        "@Process.GetCurrentProcess().ProcessName"
    </p>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
Copy the code

Running the program we see:

The process name is the name of our application. Let’s take a look at each of the other managed modes using this code as a base.

IIS

The ASP.NET Core module is required to host ASP.NET Core applications in IIS.

The ASP.NET Core module is a native IIS module that plugs into the IIS pipeline and is used to:

  • Hosting ASP.NET Core applications in the IIS worker process (w3wp.exe) is called the in-process hosting model.
  • Forwarding Web requests to back-end ASP.NET Core applications running Kestrel servers is called the out-of-process hosting model.

When hosted in-process, the module is implemented using an IIS in-process server, the IIS HTTP Server.

This module only works with Kestrel when hosted out of process. This module does not work with http.sys.

In-process hosting

ASP.NET Core applications default to the in-process hosted model. Use the IIS HTTP server (IISHttpServer) instead of the Kestrel server for in-process hosting.

The instance

The code is the same as before. First, let’s publish the code:

dotnet publish --configuration Release
Copy the code

Next, host the files in the Publish folder to IIS.

To host ASP.NET Core web sites in IIS, you need to install them first. NET Core hosts a bundle installer that restarts your computer after installation or executes from the command line:

 net stop was /y
Copy the code

Then execute:

net start w3svc
Copy the code

Then create a new website in IIS and point the root directory to the Publish folder in the previous step. Browse the website to see:

The process name is w3WP. This is the default in-process hosting mode.

Out-of-process hosting

Out-of-process hosting uses the Kestrel server instead of the IIS HTTP server (IISHttpServer).

To configure the out-of-process hosting application, set the value of the property to OutOfProcess in the project file (.csproj) :

<PropertyGroup>
  <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>
Copy the code

The w3wp Process. GetCurrentProcess (). ProcessName report/iisexpress (in-process) or dotnet (Process).

The instance

Modify myapp.csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
  </PropertyGroup>
  <ItemGroup>
  </ItemGroup>
</Project>
Copy the code

After republishing and deploying to IIS, check the website to see:

In this case, the process name is dotnet, which is in out-of-process hosting mode.

HTTP.sys

If ASP.NET Core applications run on Windows, http. sys is an alternative to Kestrel. Kestrel is usually recommended for best performance.

Http. sys runs only on Windows and cannot be used in conjunction with the ASP.NET Core module. You can use http.sys when you don’t want to use IIS or when you need functionality that Kestrel doesn’t have.

Http. sys supports the following functions:

  • Windows Authentication
  • Port sharing
  • HTTPS with SNI
  • HTTP/2 over TLS (Windows 10 or later)
  • Direct file transfer
  • In response to the cache
  • WebSocket (Windows 8 or later)

The instance

Modify the code program.cs as follows:

//using Microsoft.AspNetCore.Server.HttpSys;

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseHttpSys(options =>
                        {
                            options.AllowSynchronousIO = true;
                            options.Authentication.Schemes = AuthenticationSchemes.None;
                            options.Authentication.AllowAnonymous = true;
                            options.MaxConnections = null;
                            options.MaxRequestBodySize = 30000000;
                            options.UrlPrefixes.Add("http://localhost:5005");
                        });
                    webBuilder.UseStartup<Startup>();
                });
Copy the code

Enter to run the application on the command line.

dotnet watch run
Copy the code

Sys and the process name is myApp.

However, this method cannot be hosted in IIS, and an error will be reported if the published code is placed in IIS.

conclusion

To sum up, Kestrel is a cross-platform server, while HTTP.sys is only available on Windows. Both Kestrel and http. sys are servers embedded in ASP.NET Core, somewhat similar to Tomcat embedded in SpringBoot.

Even without the use of additional WEB servers such as IIS, Nginx, Apache, etc., the resulting files can run directly and provide HTTP services. However, the general recommended form is to use Kestrel as the application server and use the common WEB server (Nginx, Apache, etc.) as a reverse proxy.

In IIS, the ASP.NET Core module is officially provided, which makes it easy to host ASP.NET Core applications in IIS and provides two modes that work like reverse proxies. In Windows, you can use http.sys for certain functions that Kestrel does not support.