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.
Table of Contents
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.
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.
1 2 3 4 5 6 7 8 | var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.MapGet("/", () => "Hello World!"); app.Run(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); } |
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
- By Itself (Self Hosting)
- 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 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
1 2 3 | dotnet run |
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.
1 2 3 | dotnet helloWorld.dll |
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
There are many reasons why you should use this model in the production
- Security
- It can limit your exposed surface area. It provides an optional additional layer of configuration and defense.
- Simplifies Load Balancing
- 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. - Sharing Single IP with Multiple addresses
- Request FIltering, logging & URL Rewrites, etc.
- It can make sure that the application restarts if it is crashes
Benefits of Kestrel Web Server
- 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.
- It supports all the versions of .NET Core
- It is cross-platform. You can run it on Windows/Linux or Mac. This is something the ASP.NET missed always
- It is straightforward to configure and run. It is already configured when you create a new ASP.NET Core project in Visual Studio.
- Supports HTTPS
- 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.
Thanks a lot, it’s very understandable topic description.
This is wow. You made it look so simple to understand.
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.
Thanks for this tutorials, very interesting,
just to ask if you can put the date for your posts please.
thanks