Kestrel is a .NET Core cross platform web server that can be used to host web sites, web/REST services etc.
Note: This code covers .NET core 2.0 and ASP.NET core 2.0.1
Take a look at Introduction to Kestrel web server implementation in ASP.NET Core for a great post about using Kestrel along with IIS, Nginx etc.
Getting Started
Let’s get started and build a very basic application running Kestrel.
- Create a Visual C# | .NET Core | Console App (.NET Core) application
- My project is named KestrelTest
- Use NuGet to add the package Microsoft.AspNetCore
Now let’s create the most basic and most useless web server by writing the following code
using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; namespace KestrelTest { public class Startup { public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment hostingEnvironment) { } } class Program { static void Main(string[] args) { WebHost.CreateDefaultBuilder() .UseStartup<Startup>() .UseKestrel() .Build() .Run(); } } }
In Main we create the webserver and we supply the Startup application which we’d use to configure our services. Minimally we need the Configure method in the Startup object and notice we’re not adhering to any specific interface or type.
We could pass the args to the CreateDefaultBuilder if we want to start the application with certain arguments and/or use the UseKestrel override to create options for our server, but we’re aiming to just create the bare essentials of a Kestrel based web server in this post.
Now we can run this code and we’ll get, by default, a web server running and exposing port 5000 on localhost. However the server does nothing else, no static pages, nothing.
So let’s now add the NuGet package Microsoft.AspNetCore.StaticFiles and change our Configure method to look like this
public void Configure( IApplicationBuilder applicationBuilder, IHostingEnvironment hostingEnvironment) { applicationBuilder.UseWelcomePage(); }
Now run the application and using your preferred web browser, navigate to localhost:5000 and you should see a ASP.NET Core Welcome Page.
Again this is of little use but does show that everything is working.
Serving up some static content
See Work with static files in ASP.NET Core for more in depth information.
Let’s now use Kestrel to serve up some static pages. Change the Configure method to look like this
public void Configure( IApplicationBuilder applicationBuilder, IHostingEnvironment hostingEnvironment) { applicationBuilder.UseStaticFiles(); }
Now in your project folder (i.e. the folder with your Program.cs file) which should be the same as the Content root path shown when you run the application. Here you can add a wwwroot folder and place an HTML page in the folder, i.e. here’s a simply index.html file
<html> <body> Hello World </body> </html>
Make sure you set the file to have the “Copy to Output Directory” option as “Copy if Newer” in the properties window in Visual Studio.
Run the application again and navigate to localhost:5000/index.html and you should see the text Hello World.
Middleware
As we’ve seen in this post, the design of Kestrel is to supply a default web server application which we can then add our middleware code to, turning the application into a static web server or adding ASP.NET MVC into the mix etc. We can implement our own middle ware (see ASP.NET Core Middleware) which can be used to handle requests via a middleware pipeline.
I’m not going to go into any real depth in terms of developing my own middleware as we can already use various libraries for this, but just to demonstrate how we can intercept a request, change the Configure method to look like this
public void Configure( IApplicationBuilder applicationBuilder, IHostingEnvironment hostingEnvironment) { applicationBuilder.Run(async context => { await context.Response.WriteAsync("***Hello World***"); }); }
Now if we run up the application and navigate to localhost:5000 we should see ***Hello World*** returned.
References
Introduction to Kestrel web server implementation in ASP.NET Core
BenchmarksASP.NET Core Web Servers: Kestrel vs IIS Feature Comparison and Why You Need Both