Kestrel Server in ASP.NET Core

DotNet Full Stack Dev
3 min readJul 11, 2024

--

Kestrel is the default web server used in ASP.NET Core applications. It’s a cross-platform web server based on libuv, a high-performance, asynchronous I/O library. Kestrel is known for its speed and can be used as a standalone server or in combination with a reverse proxy server such as IIS, Nginx, or Apache.

Embark on a journey of continuous learning and exploration with DotNet-FullStack-Dev. Uncover more by visiting our https://dotnet-fullstack-dev.blogspot.com reach out for further information.

What is Kestrel?

Kestrel is an open-source, cross-platform web server for ASP.NET Core. It is designed to be fast and efficient, making it an ideal choice for modern web applications. Kestrel can handle HTTP requests and responses, providing a robust foundation for building web applications.

Why Use Kestrel?

  1. Performance: Kestrel is highly performant and can handle a large number of requests per second.
  2. Cross-Platform: Kestrel runs on Windows, macOS, and Linux, making it versatile for different deployment environments.
  3. Asynchronous: Built on top of libuv, Kestrel is designed to handle asynchronous I/O operations efficiently.
  4. Default Server: It’s the default web server in ASP.NET Core, which means it’s well-integrated and supported out of the box.

How to Use Kestrel?

To use Kestrel in an ASP.NET Core application, you need to configure it in the Program.cs file. Let's go through the steps to set up and use Kestrel in a new ASP.NET Core application.

Step-by-Step Implementation

1. Create a New ASP.NET Core Application

First, create a new ASP.NET Core application using the .NET CLI or Visual Studio.

dotnet new webapi -n KestrelDemo
cd KestrelDemo

2. Configure Kestrel in Program.cs

Open the Program.cs file and configure Kestrel by calling the UseKestrel method on the WebHostBuilder.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseKestrel(options =>
{
options.ListenAnyIP(5000); // Listen on port 5000
});
});
}

3. Configure the Startup Class

In the Startup.cs file, configure the services and middleware.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}

4. Create a Sample Controller

Create a sample controller to test the Kestrel server. Add a new WeatherForecastController.cs file in the Controllers folder.

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace KestrelDemo.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "Weather 1", "Weather 2" };
}
}
}

5. Run the Application

Run the application using the .NET CLI.

dotnet run

The Kestrel server will start, and the application will listen on port 5000. You can test the application by navigating to http://localhost:5000/weatherforecast in your web browser or using a tool like Postman or curl.

Conclusion

Kestrel is a powerful and efficient web server for ASP.NET Core applications. It is easy to configure and provides excellent performance. By using Kestrel, you can build high-performance web applications that are cross-platform and scalable.

Kestrel is the default web server in ASP.NET Core, and with its robust features, it is suitable for a wide range of applications. Whether you’re building a small API or a large-scale web application, Kestrel provides a solid foundation for your ASP.NET Core projects.

Understanding and configuring Kestrel is essential for ASP.NET Core developers, as it enables them to leverage the full potential of this high-performance web server. With Kestrel, you can build fast, scalable, and reliable web applications.

--

--

DotNet Full Stack Dev
DotNet Full Stack Dev

Written by DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com

No responses yet