Kestrel is open-source (source code available on GitHub), event-driven, asynchronous I/O based server used to host ASP.NET applications on any platform. It’s a listening server and a command-line interface. You install the listening server on a Windows or Linux server and the command-line interface on your computer.
It was launched by Microsoft along with ASP.NET Core. All ASP.NET Core apps utilize a new MVC framework and the Kestrel web server. These new apps can run on full .NET Framework or .NET Core. ASP.NET is used on 15% of all websites for which server-side programming languages are known, and as such, it’s important to understand how Kestrel works and the benefits it offers, so that’s why we’ve decided to take a closer look at this web server in today’s post.
A Definition of the Kestrel Web Server
Kestrel is considered a preferred web server for newer ASP.NET applications (see this post for a comparison to IIS and why you need both). It is based on the libuv library, the same one used by node.js. Libuv supports an event-driven style of programming. Some of its core utilities include:
- Non-blocking network support
- Asynchronous file system access
- Child processes
It allows ASP.NET Core applications to be run easily on other cross-platform webservers such as Nginx and Apache, without the need to address varying startup configurations. By using Kestrel as an in-process server, applications will have a consistent process (Startup (Main(), Startup.ConfigireServices(), Startup.Configure()) ) even with cross-platform support.
How Kestrel Web Server Works
Applications are often written to respond to human actions. With event-driven programming, there is a loop that listens for events. It then triggers a callback function. To reduce the number of SYS calls, all other work is executed in managed code on standard .NET worker threads.
Kestrel provides an event loop and callback-based notifications of I/O. Libuv manages the gathering and monitoring of events from the OS. Moreover, the user can register callbacks as an event occurs. So, Kestrel uses libuv for I/O work and supports running multiple event loops.
Because it is so lightweight, Kestrel does not allow you to do SSL termination, URL rewrites, or GZip compression, but that same lightweight design can make other web servers look slow by comparison. It was built to be fast; in fact, it’s six times faster than node.js for static and plain text operations.
Benefits of Kestrel
Kestrel has support on all platforms and versions supported by .NET Core. Furthermore, it is included by default in ASP.NET Core new project templates and can provide better request processing performance. When you create a new project in Visual Studio, your project is automatically configured to run in Kestrel.
As stated earlier, it’s not a fully-featured web server, but that’s precisely why it’s fast. If you feel the need for speed, Kestrel is the answer–especially since it is designed to be used in production for ASP.NET.
What you can do is run it behind a more fully-featured webserver such as IIS or NGNIX. You can run it behind IIS using the HttpPlatformHandler or behind IIS Express using HttpPlatformHandler by Visual Studio. Moreover, you’ll want to support it in your ASP.NET Core projects so that they can be conveniently run by developers on any of the supported platforms.
Now, even if you are not working cross-platform, you can run ASP.NET on a web server straight from the command line.
Since Kestrel is not a fully-featured web server, you should run it behind IIS or NGNIX. It was designed to make ASP.NET as fast as possible but is limited in its ability to manage security and serve static files. If you’re using ASP.NET Core with Kestrel, you can take advantage of Prefix for powerful code profiling.
Additional Resources and Tutorials
For more information, including some helpful tutorials, visit the following resources:
- Web server implementations in ASP.NET Core
- How to modify code in Microsoft.AspNetCore.Server.Kestrel and then refer to it from elsewhere?
- Set up a hosting environment for ASP.NET Core on Windows with IIS, and deploy to it
- Constructing Test Cases That Don’t Suck (and How to Avoid Common Mistakes) - August 21, 2017
- 35 Leading PaaS Providers Offering Built-In Infrastructure and Scalability - August 21, 2017
- Biggest Mistakes Companies Make When Evaluating & Purchasing APM Software - August 18, 2017
- Why Security Should be Top-of-Mind for Developers - August 16, 2017
- ViewBag 101: How It Works, When It’s Used, Code Examples, and More - August 11, 2017