Kestrel: Web Server for ASP.NET Core

In this tutorial, let us look at the Kestrel Web Server for ASP.NET Core. The way we host our application in ASP.NET Core has gone through some drastic change compared to the previous version of ASP.NET. The Kestrel is the new default web server that is included in the ASP.NET Core project templates. It runs within the application process making it completely self-contained.

What is Kestrel?

The Kestrel is open-source, cross-platform, event-driven, asynchronous I/O based HTTP server. It is developed to host ASP.NET Core applications on any platform. It is included by default in the ASP.NET Core applications.

It is based on libuv.

Kestel is an Open source library and is available at GitHub.

Why Kestrel

The older ASP.NET applications are tightly coupled to the Internet Information Services or IIS.

The IIS is a complete web server with all the features you require from a Web Server. Over the period, it has grown into a mature web server, and along the way, it has added a lot of weight and bloat. It has become one of the best Web servers around, and at the same time, it is one of the slowest.

ASP.NET being tightly coupled with IIS carried the burden of the IIS.

The newly designed ASP.NET Core applications are now completely decoupled from the IIS. This decoupling makes ASP.NET Core run on any platform, making it truly cross-platform.

But, it still needs to be able to listen to HTTP requests and send the response back to the Client. That is where Kestrel comes in.

How it Works

Kestrel web server is embedded into ASP.NET Core applications and runs in-process with the Application. Hence, It runs independently of the environment in which it lives.

ASP.NET Core applications on startup launch a host. You can think of a host as a wrapper around your application. It is responsible for the startup and lifetime management of the application. The host among the other things also launches Kestrel server. The kestrel Web server listens for requests and sends responses.

Kestrel Web server

The Kestrel Web server is available in the namespace Microsoft.AspNetCore.Server.Kestrel

Take a look at the program.cs from the ASP.NET Core 6.0 and above (ASP.NET Core Empty template). It is the entry point for the ASP.NET Core application.

The WebApplication.CreateBuilder method registers and configures the Kestrel HTTP server. And when we run the command app.Run it will start listening for HTTP requests.

The following code is from the Program class from ASP.NET Core versions before 6.0. The contains a static void Main method, which serves as the application’s entry point.

The Main method invokes CreateDefaultBuilder, which is responsible for creating the host. The CreateDefaultBuilder is a helper method ( click here for the source code). It calls the UseKestrel method to register the Kestrel as the server.

CreateDefaultBuilder creates the Web Host and returns it. The Kestrel web server listens for HTTP requests when we invoke the Run method to start the App.

There are two ways you can use the Kestel

  1. By Itself (Self Hosting)
  2. Behind another Webserver

Self Hosting

Under Self Hosting model the ASP.NET Core applications directly listen to the HTTP Requests from the internet as shown in the image below.

The diagram shows how Kestrel Web server is used to self host a ASP.NET Core Application
The diagram shows how Kestrel Web server is used to self-host an ASP.NET Core Application

The following image shows how to use dotnet run cli command to start the kestrel web server. To run all you need to do is to go to the project folder, where .csproj file is found and run the following command

The kestrel starts and listens on port 5000/5001.

Another way is to go to the published folder and run the following command, which will fire up the kestrel server.

Behind another Webserver

Kestrel is not a fully-featured web server. But that is what makes it fast.

Running Kestrel as a standalone web server in the Production environment is not advisable. Running it behind a Fully Featured Web server like IIS, Nginx, Apache, etc., is recommended. In such a scenario, the Web server acts as a reverse proxy server.

The reverse proxy server takes the HTTP request from the internet and passes it to the kestrel server just as it is received.

The IIS can take the HTTP request and perform helpful processing like logging, filtering, and URL rewrites before passing the request to Kestrel.

The following diagram shows how it is implemented

Kestrel Web server running behind another Web server like IIS, NGINX, Apache, etc
Kestrel Web server running behind another Web server like IIS, NGINX, Apache, etc

There are many reasons why you should use this model in the production

  1. Security
  2. It can limit your exposed surface area. It provides an optional additional layer of configuration and defense.
  3. Simplifies Load Balancing
  4. SSL Setup
    Only your reverse proxy server requires an SSL certificate, and that server can communicate with your application servers on the internal network using plain HTTP.
  5. Sharing Single IP with Multiple addresses
  6. Request FIltering, logging & URL Rewrites, etc.
  7. It can make sure that the application restarts if it is crashes

Benefits of Kestrel Web Server

  1. Kestrel is fast. It is not a fully-featured web server and does not provide the many features you expect from a standard web server. Hence it makes it lightweight in design and fast.
  2. It supports all the versions of .NET Core
  3. It is cross-platform. You can run it on Windows/Linux or Mac. This is something the ASP.NET missed always
  4. It is straightforward to configure and run. It is already configured when you create a new ASP.NET Core project in Visual Studio.
  5. Supports HTTPS
  6. Supports HTTP/2 (except on macOS. But will be supported in a future release).

Alternatives to Kestrel

The Kestrel is not the only way to host ASP.NET Core applications. There is another web server implementation available in Windows known as HTTP.SYS

The HTTP.sys  is a Windows-only HTTP server based on the Http.Sys kernel driver.

Here is a nice tutorial on How to use HTTP sys.

Summary

In this tutorial, we learned about the Kestrel Web server. Kestrel is an In-Process web server built to run ASP.NET Core applications on any Platform.

4 thoughts on “Kestrel: Web Server for ASP.NET Core”

  1. How to configure IIS as reverse proxy for a asp .net core front end with multiple internal services. IIS required to have site and host the front end application or front end also hosted by Kestrel server.

  2. Thanks for this tutorials, very interesting,
    just to ask if you can put the date for your posts please.
    thanks

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top