(C# ASP.NET Core) Kestrel, IIS, IISHttpServer and HTTP.sys

An ASP.NET Core application cannot directly accept requests from the internet. For this it needs a server that can face the internet and help it communicate with the calling client on the other side. This means that some sort of coupling is required with a server software. This tutorial discusses various possibilities. Discussion of the launchSettings file is reserved for the next tutorial.
(Rev. 18-Jun-2024)

Categories | About |     |  


Method 1: Self Contained Mode

One method is to run it as a self-contained application through an integrated server called Kestrel. This method doesn't require any external server such as IIS, etc., I will come to the details of Kestrel in a moment.

Stand-alone mode is mainly used for testing purposes because of the console window that opens alongside. It is not used in production scenarios because Kestrel is not a sophisticated server.

Let me first show how to run your application in a stand-alone mode.

When you build an ASP.NET Core application, a dll and an exe file are created for you.

Let us open the output directory to have a look at the dll and executable.

You can either double-click the executable or open a command prompt and run the command dotnet path-to-dll

When the application runs it launches Kestrel automatically. A console window opens, and a running log of messages also appears. It can be a convenient view for seeing exceptions and errors.

We can open the browser to verify that the app responds to our requests.

Video Explanation

Please watch the following youtube video:

A Quick Introduction to Kestrel

Let us have a look at a brief introduction to the features of Kestrel:

  1. Kestrel is a web server software. And like every server, it can listen for http requests.
  2. Kestrel is cross platform - so it can work on linux also.
  3. It is based on an open source library called libuv.
  4. It is included and enabled by default when an ASP.NET Core app is created through project templates, such as through Visual Studio. No special effort or steps are required for installing or configuring it.
  5. Kestrel has been written and adapted by microsoft specifically for use with an asp.net core web application.

Method 2: Kestrel behind an IIS Proxy (OutOfProcess)

Let us come to the second method now.

We know that Kestrel is neither too advanced, and nor is it designed for facing the internet directly. Hence, a better method could be to use Kestrel as the backend of an IIS server - i.e., to use IIS as a reverse proxy for Kestrel.

Thus, an ASP.NET Core application can be deployed to run with an IIS + Kestrel combination. In this case the IIS faces the internet and forwards requests to the Kestrel server running through your ASP.NET Core application.

Requests come from the internet, then they reach IIS which makes a loopback localhost request to Kestrel. Kestrel is running as an independent process. Then Kestrel forwards it to your application. Your application processes it, and sends the response to Kestrel, which in turn sends it to IIS, and then to the client.

This scheme was the default and recommended scheme before version 2.2 of ASP.NET Core. The problem with this scheme is that it is slower because IIS and Kestrel run in two different processes and a time lag becomes noticeable on heavier traffic.

It is not the default scheme anymore now. But it is still supported, especially if the hosting is on a linux Apache, Nginx (pronounced as engine x).

This scheme needs to be enabled through the csproj file.

For this open the solution explorer. Right click the project to open the context menu. Select Edit Project File and click it. The project settings open in an XML format.

Add the setting AspNetCoreHostingModel to OutOfProcess.

All the necessary port settings, environment variables, etc., between IIS and Kestrel will be automatically made when this application is published the next time. So it is just a matter of changing AspNetCoreHostingModel.

Note: Kestrel can be run behind Apache, Nginx in similar manner if the deployment is on linux.

Method 3: IIS with IISHttpServer

The third method, and the most used one, is to use a module called IISHttpServer - which replaces the use of kestrel altogether.

This is a faster alternative to using Kestrel + IIS. In this method Kestrel is not used.

This is now the default setting. The value AspNetCoreHostingModel is InProcess by default after ASP.NET Core 2.2.

ASP.NET Core now includes a new module called IISHttpServer that runs in the same process as the IIS. It is different from the situation with Kestrel. Kestrel runs as an independent process, but IISHttpServer loads in the same process as IIS. For more technical details you can refer the msdn documentation of IISHttpServer.

The advantage is that the application becomes faster, because there is no out of process communication to an external instance.

Method 4: with Http.sys

This is an HTTP server based on the http.sys kernel mode driver. It is available only on windows.

It is an alternative to Kestrel, as well as the IIS. It can be used for intranet hostings, or in situations where a feature is not available in Kestrel. You will have to refer the msdn documentation for finer details.

In the next tutorial we shall explore how to use launchSettings file for running an ASP.NET Core application through Visual Studio.

This Blog Post/Article "(C# ASP.NET Core) Kestrel, IIS, IISHttpServer and HTTP.sys" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.