Kestrel Web Server in ASP.NET Core Application
ASP.NET Core is a Cross-Platform framework. It supports developing and running applications on operating systems such as Windows, Linux, or MacOS.
The Kestrel is the Cross-Platform Web Server for the ASP.NET Core Web Application. This Server supports all the platforms and versions that the ASP.NET Core Supports.
By default, Kestrel is included as the Internal Web Server in the .NET Core application.
But if you want, you can even use Kestrel as the Internet Facing Web Server, i.e., External Web Server.
Using Kestrel as External Web Server
In this case, the Kestrel Web Server is used as an Edge Server, i.e., the internet-facing web server, which will directly process the incoming HTTP request from the client.
In the case of the Kestrel web server, the process name that is used to host and run the ASP.NET Core application is nothing but the project name.
Kestrel is the default web server that comes with ASP.NET Core, and it can be used standalone or in combination with other web servers like IIS or Nginx.
Kestrel is a lightweight, cross-platform web server built specifically for ASP.NET Core applications.
It’s designed to be a fast, scalable, and efficient web server that can handle incoming HTTP requests and serve content to clients.
Run Applications Using Kestrel Web Server in ASP.NET Core
Expand Properties folder in solution explorer >> open the launchSettings.json file
In our example, for IIS Express, the port number is 36312 for HTTP and 44354 for HTTPs, and the worker process is iisExpress while for the Kestrel Server, the port number is 5164 for HTTPs and 7164 for HTTPS, and the worker process name is WebApplication3
Here, you can see we have two sections. One is for IIS Express (IIS Server), and the other is for the Kestrel server. You can find the above two profiles (IIS Express and FirstCoreWebApplication) in Visual Studio as well
If you select IIS Express, it will use the IIS Server; if you select WebApplication3, it will use Kestrel Server.
To use the Kestrel Server to run your application in Visual Studio, you must first select the WebApplication3 profile
Once you select the WebApplication3, run the application. Here, we need to observe two things.
- First, it will launch the command prompt and host the application using the Kestrel Web Server, Cross check URL and Port Number in command prompt (as as in launch.json file).
- Secondly, it opens the default browser and listens to that URL and Port Number.
Using the browser developer tool, You can verify whether it is using the Kestrel Web Server. Run the application and open the browser developer tool by pressing the F12 key, then go to the Network tab and again issue the same request,
In this case, Kestrel is the only Web Server hosting our ASP.NET Core Web Application and Handling the incoming HTTP Requests. We don’t need to install Kestrel Web Server additionally. It is by default included in ASP.NET Core, and this Kestrel Web Server makes ASP.NET Core Cross Platform.
Key Features and Characteristics of Kestrel:
Cross-Platform: Kestrel is fully cross-platform and can run on Windows, Linux, and macOS. It’s designed to work seamlessly across different operating systems.
Performance: Kestrel is optimized for performance and is capable of handling a large number of concurrent connections efficiently. It’s particularly well-suited for serving static files and handling lightweight workloads.
Asynchronous I/O: Kestrel is built using async programming patterns, allowing it to handle many requests with fewer threads, leading to better resource utilization and responsiveness.
Self-Hosting: Kestrel can be a standalone web server without additional software. This makes deploying and running ASP.NET Core applications on different platforms easy.
Support Reverse Proxies: While Kestrel is a capable web server, it’s often used with reverse proxy servers like Nginx or Apache. Reverse proxies handle tasks like SSL termination, load balancing, security,
HTTPS Support: Kestrel supports HTTPS, allowing you to secure your applications with SSL/TLS encryption easily.
Hosting Model: While Kestrel is the default web server, it’s often used with other web servers like IIS or Nginx. Kestrel acts as an internal server while the external web server handles tasks like load balancing and SSL termination.
Advantages of Kestrel:
Cross-Platform: Since Kestrel is built on .NET Core, it can run on various platforms, including Windows, macOS, and Linux, allowing for more deployment flexibility.
Performance: Kestrel is optimized for high throughput and low latency, making it one of the fastest web servers. Its non-blocking, event-driven model allows it to handle many connections simultaneously.
Modular and Lightweight: Kestrel is designed to be minimalistic and modular. You only include and pay for the necessary components, reducing the overhead and potential attack surface.
Integration with ASP.NET Core: Kestrel is tightly integrated with the ASP.NET Core infrastructure, leading to seamless interaction and configuration using standard ASP.NET Core methods.
Flexibility in Hosting Models: With Kestrel, you can run your ASP.NET Core app as a standalone service or behind a more robust web server, like IIS, Nginx, or Apache, which can act as a reverse proxy.
Disadvantages of Kestrel:
Edge Server Limitations: Kestrel was not recommended to be exposed directly to the internet (as an “edge” server) without a reverse proxy in front of it. While Kestrel is improving and gaining more features, it doesn’t support few important features (like request filtering) or defense in depth against attacks that more mature web servers like IIS, Nginx, or Apache have.
Less Mature: While Kestrel has come a long way and is production-ready, it’s newer than other web servers like IIS or Apache. It might not have all the features or extensive real-world battle testing that these older servers have experienced.
Complexity in Production: If you’re deploying to a scenario where Kestrel needs a reverse proxy (e.g., IIS or Nginx), it can introduce an extra layer of configuration and potential points of failure.
Memory Management: Kestrel’s performance gains come partly from pooling large objects to reduce garbage collection. However, this can increase memory consumption in specific scenarios if not managed correctly.