ASP.NET Core with Kestrel Webserver
The 1st question comes to my mind when I hear about another new webserver like IIS for .NET applications. Why do we need this new webserver called 'Kestrel'?
As we all know Asp.Net Core is primarily to make Asp.Net Core applications to run across multiple platforms (Windows/Unix/Linux/MAC OS). Even though we have IIS with rich in features but mostly used webserver for Windows OS only. Each webserver has a different configurations expected to startup and Kestrel will make Asp.Net Core applications have different Startup mechanisms. This is why Asp.Net Core applications use Kestrel webserver as an in-process server where the application will have same and consistent Startup (Main() and Startup.ConfigireServices() & Startup.Configure()) process even when offering cross platform support.
Kestrel is an open source, cross platform, light weight and a default webserver used for Asp.Net Core applications. Asp.Net Core applications run Kestrel webserver as in-process server to handle web request. Kestrel webserver is based on async I/O library called 'libuv' primarily developed for Node.js.
By default, all Asp.Net core project templates include Kestrel webserver when we create new Asp.Net Core Projects. As said before, Kestrel is a very light weight webserver that does not have every advanced features of webservers like IIS, Nginx, Apache, etc has. Due to its lightweight nature, Kestrel provides better request processing performance to Asp.Net Core applications. Some features of Kestrel,
Kestrel server is by default included in the Asp.Net Core project templates. It is included in the project as a Nuget package Microsoft.AspNetCore.Server.Kestrel in Project.json (in Visual Studio 2015) and as an implicitly included package when you using Visual Studio 2017 project template.
Note – All Asp.Net Core project should use Visual Studio 2017 as it is the latest and default project template for Asp.Net Core projects henceforth.
Let’s see how kestrel server is configured in an Asp.Net Core project. Below is the Program.Main() method in a Asp.Net Core application.
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup()
.UseApplicationInsights()
.Build();
host.Run();
}
}
We generally deploy Asp.Net Core (also Asp.Net applications) for 2 scenarios listed below.
Internally Hosted applications
For internal applications, the applications can just be hosted with Kestrel webserver alone. The features provided by Kestrel and with some additional middlewares (for response compressions, authentication) is sufficient for serving internally hosted applications.
Externally Hosted applications
As it is said before, Kestrel is a very light webserver and it does not support all features of a full-fledged webserver like IIS provide. So, if your application is external facing public website then the application should be deployed behind a full-fledged webserver like IIS or Nginx or Apache for increased security and to get feature rich support. These webservers act as proxy (commonly called reverse proxy in this scenario) and forward the request to Kestrel for request processing. This is why the Program.Main() code calls UseIISIntegration() to integrate IIS as reverse proxy to forward request to Kestrel. This is the default mode the project template code uses.
As we all know Asp.Net Core is primarily to make Asp.Net Core applications to run across multiple platforms (Windows/Unix/Linux/MAC OS). Even though we have IIS with rich in features but mostly used webserver for Windows OS only. Each webserver has a different configurations expected to startup and Kestrel will make Asp.Net Core applications have different Startup mechanisms. This is why Asp.Net Core applications use Kestrel webserver as an in-process server where the application will have same and consistent Startup (Main() and Startup.ConfigireServices() & Startup.Configure()) process even when offering cross platform support.
Kestrel is an open source, cross platform, light weight and a default webserver used for Asp.Net Core applications. Asp.Net Core applications run Kestrel webserver as in-process server to handle web request. Kestrel webserver is based on async I/O library called 'libuv' primarily developed for Node.js.
By default, all Asp.Net core project templates include Kestrel webserver when we create new Asp.Net Core Projects. As said before, Kestrel is a very light weight webserver that does not have every advanced features of webservers like IIS, Nginx, Apache, etc has. Due to its lightweight nature, Kestrel provides better request processing performance to Asp.Net Core applications. Some features of Kestrel,
- Kestrel does not support multiple applications sharing same port similar to IIS where applications are differentiated by host header value.
- Kestrel is cross platform, runs in Windows, LINUX and Mac.
- Kestrel webserver supports SSL.
Kestrel server is by default included in the Asp.Net Core project templates. It is included in the project as a Nuget package Microsoft.AspNetCore.Server.Kestrel in Project.json (in Visual Studio 2015) and as an implicitly included package when you using Visual Studio 2017 project template.
Note – All Asp.Net Core project should use Visual Studio 2017 as it is the latest and default project template for Asp.Net Core projects henceforth.
Let’s see how kestrel server is configured in an Asp.Net Core project. Below is the Program.Main() method in a Asp.Net Core application.
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup
.UseApplicationInsights()
.Build();
host.Run();
}
}
We generally deploy Asp.Net Core (also Asp.Net applications) for 2 scenarios listed below.
Internally Hosted applications
For internal applications, the applications can just be hosted with Kestrel webserver alone. The features provided by Kestrel and with some additional middlewares (for response compressions, authentication) is sufficient for serving internally hosted applications.
Externally Hosted applications
As it is said before, Kestrel is a very light webserver and it does not support all features of a full-fledged webserver like IIS provide. So, if your application is external facing public website then the application should be deployed behind a full-fledged webserver like IIS or Nginx or Apache for increased security and to get feature rich support. These webservers act as proxy (commonly called reverse proxy in this scenario) and forward the request to Kestrel for request processing. This is why the Program.Main() code calls UseIISIntegration() to integrate IIS as reverse proxy to forward request to Kestrel. This is the default mode the project template code uses.
Comments