Understanding Servers in ASP.NET Core.

In ASP.NET Core, a server is a critical component responsible for listening to incoming HTTP requests, processing them, and then returning appropriate responses. It acts as the bridge between the outside world (browsers, mobile apps, API consumers) and your ASP.NET Core application.


When you build a web application using ASP.NET Core, your application doesn't just sit there waiting; it needs to run inside a web server environment like Kestrel, IIS, or HTTP.sys that continuously listens for incoming network requests.


ASP.NET Core applications are self-hosted, meaning they don’t rely on the legacy IIS pipeline. Instead, they can run on any server that can host a .NET Core process. This cross-platform model allows the application to be hosted on Windows, Linux, or macOS, making ASP.NET Core truly modern and flexible.

What is a Server in ASP.NET Core?

In ASP.NET Core, a server is a component responsible for:

  • Listening for HTTP requests
  • Forwarding Requests to the Application Pipeline
  • Returning HTTP responses to the client
  • Handling Protocols and Security
  • Logging, Monitoring, and Resource Management

Unlike the traditional ASP.NET (pre-Core), which is tightly integrated with IIS, ASP.NET Core is self-hosted, meaning it can run independently of IIS and on any platform (Windows, Linux, macOS). This makes it ideal for cloud-native applications and containerized deployments.

Types of Servers in ASP.NET Core.

One of its biggest strengths of ASP.NET Core is its flexible hosting model, which allows developers to choose how their apps are hosted and served.

When it comes to hosting an ASP.NET Core application, you have three primary server options:

  • Kestrel
  • IIS (Internet Information Services)
  • HTTP.sys

Let’s dive into each one in detail.

1. Kestrel Server (Default and Cross-platform)

Kestrel is the default web server included with ASP.NET Core. It’s a lightweight, cross-platform, high-performance web server designed to serve ASP.NET Core applications either directly or behind a reverse proxy (like Nginx or IIS).

Features:

  • Supports HTTP/1.1, HTTP/2, and HTTPS
  • Fully asynchronous I/O for better scalability
  • Works on all platforms (Windows, Linux, macOS)
  • Designed to be fast and minimal
  • Handles WebSockets

Configuration Example for Kestrel Server. (Program.cs in .NET 6+)

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(serverOptions =>
{
    serverOptions.Limits.MaxRequestBodySize = 10 * 1024; // 10 KB
    serverOptions.ListenAnyIP(5001); // HTTP
    serverOptions.ListenAnyIP(5002, listenOptions =>
    {
        listenOptions.UseHttps(); // HTTPS
    });
});

var app = builder.Build();
app.Run();
Kestrel is Ideal for running web apps and APIs in Docker, Kubernetes, or Linux environments. Commonly used behind Nginx or IIS in production. 

In production-grade ASP.NET Core deployments, developers don’t expose the Kestrel server directly to the internet. Instead, they use a more mature and robust reverse proxy server like Nginx (on Linux) or IIS (on Windows) in front of the Kestrel server. This setup has several benefits related to security, performance, and scalability.

2. IIS (Internet Information Services)

IIS (Internet Information Services) is a powerful, flexible, and secure web server developed by Microsoft for hosting web applications on the Windows platform. In the context of ASP.NET Core, IIS no longer runs your application directly as it did in the older .NET Framework. Instead, it acts as a reverse proxy server that forwards HTTP requests to the Kestrel server, which is the actual web server running the ASP.NET Core application.

When hosting an ASP.NET Core application with IIS, a special component called the ASP.NET Core Module (ANCM) comes into play. This module is installed as part of the .NET Hosting Bundle and is responsible for starting the application and routing all incoming HTTP traffic from IIS to the internal Kestrel process. This design ensures that IIS can still provide enterprise-grade features like Windows Authentication, URL rewriting, SSL termination, application pool isolation, and centralized logging, while leveraging Kestrel’s performance and cross-platform benefits in the background.

IIS supports two hosting models for ASP.NET Core applications: in-process and out-of-process. In in-process hosting, the application runs within the IIS worker process (w3wp.exe), providing better performance and lower overhead. This is the recommended and default mode since ASP.NET Core 2.2. On the other hand, out-of-process hosting runs the application as an external process (using dotnet MyApp.dll), and IIS simply forwards requests to it. While still functional, this mode introduces an extra communication layer and is less efficient compared to in-process hosting.

Configuration of IIS in ASP.NET Core.

To configure IIS for an ASP.NET Core application, you need to install the .NET Core Hosting Bundle on your Windows server, publish your application, and set up a website in IIS that points to the published folder. 

IIS acts as a reverse proxy, forwarding requests to the Kestrel server via the ASP.NET Core Module, using the settings defined in the web.config file. This allows your ASP.NET Core app to run smoothly on IIS while benefiting from features like Windows Authentication, SSL termination, and centralized management.

<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="dotnet" arguments="MyApp.dll" hostingModel="inprocess" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
  </system.webServer>
</configuration>

3. HTTP.sys Server (Windows-only Alternative)

HTTP.sys is a Windows-only web server that ASP.NET Core can use as an alternative to Kestrel. It’s built on the Windows kernel-mode HTTP.sys driver, which offers advanced features like Windows Authentication (NTLM/Kerberos), SSL termination, port sharing, and request queuing. Unlike Kestrel, it doesn't require a reverse proxy (like IIS or Nginx) and can handle HTTP requests directly. HTTP.sys is ideal for intranet applications or services that need deep Windows integration without the overhead of IIS.

Configuration Example of HTTP.sys.

builder.WebHost.UseHttpSys(options =>
{
    options.Authentication.Schemes = AuthenticationSchemes.NTLM;
    options.UrlPrefixes.Add("http://localhost:5000");
});

How to choose the right server?

Below are some popular use cases and server recommendations that you can follow for choosing the right server for your ASP.NET Core application:
Deployment Scenario Recommended Server
Cross-platform deployments Kestrel
Windows enterprise environments IIS (with Kestrel)
Windows-only services HTTP.sys
Containerized environments Kestrel + Nginx
Lightweight API backend Kestrel

Conclusion

Understanding how servers work in ASP.NET Core is crucial for building reliable, high-performance web applications. Whether you're deploying on Windows with IIS or on Linux with Kestrel and Nginx, ASP.NET Core provides the flexibility and control to choose the right server architecture for your application.

Key Takeaways:
  • Kestrel is the default, cross-platform server.
  • IIS and HTTP.sys are Windows-specific options.
  • Use reverse proxies for better security and performance.
  • Server configuration affects scalability, security, and performance.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS