Loading

ASP.NET Core

What is Kestrel Web Server in ASP.NET Core?. The Complete ASP.NET Core Developer Course 2023 [Videos].

Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the web server thats included by default in ASP.NET Core project templates. Kestrel supports the following scenarios: HTTPS. Opaque upgrade used to enable WebSockets.

Kestrel Web Server in ASP.NET Core Application

In this Video, I am going to discuss the Kestrel Web Server in ASP.NET Core Application.  At the end of our previous Video, we discussed that with the OutOfProcess hosting model, there are 2 web servers i.e. one internal web server and one external web server. The internal web server is called Kestrel and the external web server can be IIS, Apache, or Nginx. As part of this Video, we are going to discuss the following two important concepts in detail.

  1. What is a Kestrel Web Server?
  2. How to Configure Kestrel Web Server?
  3. How to run a .NET Core application using Kestrel Web Server?
  4. How to run a .NET Core Application using .NET Core CLI.

What is a Kestrel Web Server?

As we already discussed ASP.NET Core is a cross-platform framework. It means it supports to development and run applications on different types of operating systems such as Windows, Linux, or Mac.

The Kestrel is the cross-platform web server for the ASP.NET Core application. That means this Server supports all the platforms and versions that the ASP.NET Core supports. By default, it is included as the internal web server in the .NET Core application.

The Kestrel Web Server is generally used as an edge server i.e. the internet-facing web server which directly processes 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 the project name.

As of now, we are using visual studio to run the ASP.NET Core application. By default, the visual studio uses IIS Express to host and run the ASP.NET Core application. So, the process name is IIS Express that we already discussed in our previous Video. 

How to run the application using Kestrel Web Server?

Before using the Kestrel server to run our application, let us first open the lauchsetting.json file which is present inside the Properties folder of your application. Once you open the launchSettings.json file you will find the following code by default.

How to run the application using Kestrel Web Server?

In our upcoming Video, we will discuss lauchsetting.json in detail. But for now, just have a look at the Profiles section. Here, you can see, we have two sections. One is for IIS Express (IIS Server) and the other one is for the Kestrel server. In visual studio, you can find the above two profiles (IIS Express and FirstCoreWebApplication) as shown below.

What is a Kestrel Web Server?

If you select IIS Express then it will use the IIS server and if you select FirstCoreWebApplication, then it will use Kestrel Server.

Running the application using IIS Express:

If you run the application using IIS Express, then it will use the URL and port number mentioned in the iisSettings of your launchSettings.json file. To prove this run the application using IIS Express and see the output as shown below.

Running the application using IIS Express

Running the application using Kestrel Server:

In order to use the Kestrel server to run your application in Visual Studio, first, you need to select the FirstCoreWebApplication profile as shown below.

Running the application using Kestrel Server

Once you select the FirstCoreWebApplication, now run the application. Here, we need to observe two things. First, it will launch the command prompt and host the application using the Kestrel server as shown below. Here, you need to focus on the URL and port number and it should be the URL and port number mentioned in your FirstCoreWebApplication profile of launchSettings.json file.

How to Configure Kestrel Web Server?

Secondly, it opens the default browser and listening to that URL and Port Number as shown below.

How to Configure Kestrel Web Server in Visual Studio?

Note: In our example, for IIS Express the port number is 60211, and the worker process is iisexpress while for the Kestrel server the port number is 5000 and the worker process name is FirstCoreWebApplication (It is nothing but your application name).

How to run a .NET Core application using .NET Core CLI?

We can also run the ASP.NET Core application from the command line using the .NET Core CLI. The CLI stands for Command Line Interface.

When we run an ASP.NET Core application using the .NET Core CLI, then the .NET Core runtime uses Kestrel as the webserver. We will discuss the .NET Core CLI in detail in our upcoming Video. Now, let us see how to run a dot net core application using .NET Core CLI Command.

First. open the command prompt and type “dotnet —” and press enter as shown below.

How to run .NET Core application using .NET Core CLI?

Once you type the “dotnet —” and click on the enter button then you will find lots of commands as shown below.

.NET Core CLI Command

Using the CLI (above commands)

You can create a new project using the new command, you can also build the project using the build command, or you can publish the project using the publish command. It is possible to restore the dependencies and tools which are required for a .net core project using the CLI.

Running .NET Core application using .NET Core CLI

Lets see how to run a .NET Core application using the .NET Core CLI command. To do so please follow the below steps

First, open the Windows Command Prompt. To do so, open the run window and then type cmd and click on the enter button which will open the command prompt. Then you need to change the directory to the folder which contains your asp.net core application. My project is present in the “D:ProjectsCoreFirstCoreWebApplicationFirstCoreWebApplication” folder so I change the current directory to my project file by using the following command.

Running .NET Core application using .NET Core CLI

Once you change the directory to your project folder, then execute the “dotnet run” command as shown in the below image.

dotnet run command

Once you type the dotnet run command, press the enter key, then the .NET Core CLI builds and runs the application. It also shows the URL and you can use this URL to access your application as shown in the below image.

Kestrel Web Server in ASP.NET Core Application

.Here, in my case, the application is available at http://localhost:5000If you remember this port is configured in the launchSettings.json file of your application inside the FirstCoreWebApplication profile which is nothing but the profile for the Kestrel server. Now open the browser and navigate to the http://localhost:5000 URL and It should display the worker process name as dotnet as shown below.

Kestrel Web Server

Changing the Port Number:

If you want then you can also change the Port number for Kestrel Server. To do so open the launchSettings.json file and give any available Port number as shown below. Here, I am changing the Port number to 60222.

Changing the Port Number

Now, save the changes and run the application using Kestrel Server and you should see the changed port number in the URL.


See All

Comments (232 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Core / Blog

What is ASP.NET Core?

ASP.NET Core is the new version of the ASP.NET web framework mainly targeted to run on .NET Core platform.
27-jan-2022 /16 /232

ASP.NET Core / Blog

What is ASP.NET Core Framework?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /232

ASP.NET Core / Blog

How to Setup ASP.NET Core Environment ?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /232