ASP.NET Core Hosting

Learn how to deploy and host your ASP.NET Core applications.

Introduction to ASP.NET Core Hosting

ASP.NET Core applications are designed to be cross-platform and flexible in their deployment. Understanding the hosting models and options is crucial for successfully deploying and managing your applications in production. This document covers the various ways to host your ASP.NET Core applications, from local development to cloud environments.

Hosting Models

ASP.NET Core applications can be hosted in two primary ways:

  • In-Process Hosting: The ASP.NET Core Module (ANCM) hosts the application directly within the IIS worker process. This model offers lower latency as it avoids inter-process communication.
  • Out-of-Process Hosting: The application runs in a separate process hosted by Kestrel, and IIS acts as a reverse proxy. This is the default and most common configuration.

The choice between these models depends on your specific needs regarding performance, security, and existing infrastructure.

Hosting on Internet Information Services (IIS)

IIS is a popular web server for Windows environments. ASP.NET Core can be hosted on IIS using the ASP.NET Core Module (ANCM).

Configuring ANCM

The ANCM is installed as part of the .NET Core Windows Hosting Bundle. For out-of-process hosting, ANCM forwards requests to Kestrel.

The web.config file plays a vital role in configuring ANCM. Here's a typical configuration for out-of-process hosting:

<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet"
                arguments=".\YourApp.dll"
                stdoutLogEnabled="false"
                stdoutLogFile=".\logs\stdout"
                hostingModel="OutOfProcess" />
  </system.webServer>
</configuration>
Note: Ensure that the processPath and arguments correctly point to your application's executable DLL. For in-process hosting, change hostingModel to InProcess.

Kestrel Web Server

Kestrel is ASP.NET Core's cross-platform, high-performance, and default web server. It can be used directly or behind a reverse proxy.

Configuring Kestrel

Kestrel's configuration is typically handled in the Program.cs or Startup.cs file (depending on your ASP.NET Core version). You can configure ports, endpoints, and other network settings.

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.ListenLocalhost(5000); // HTTP on port 5000
                    options.ListenLocalhost(5001, configure => configure.UseHttps()); // HTTPS on port 5001
                });
            });
}
Important: When hosting Kestrel directly, ensure you have a firewall configured to allow incoming traffic on the specified ports.

Using Reverse Proxies

It's common practice to host ASP.NET Core applications behind a reverse proxy like Nginx, Apache, or cloud-based load balancers. The reverse proxy handles tasks such as SSL termination, load balancing, caching, and serving static files.

Benefits of Reverse Proxies:

  • Security: Hides the application server and can provide additional security layers.
  • Scalability: Enables load balancing across multiple application instances.
  • Performance: Can cache static content and compress responses.
  • SSL Termination: Offloads SSL/TLS encryption/decryption from the application.

Deployment Options

ASP.NET Core applications can be deployed to various environments:

  • Self-Contained Deployment: The .NET runtime is included with the application. No prior .NET Core installation is needed on the target machine.
  • Framework-Dependent Deployment: The application depends on a .NET runtime already installed on the target machine. This results in smaller deployment packages.
  • Containerization: Using Docker and containers (like Kubernetes) provides a consistent and isolated environment for deployment across different platforms.
  • Cloud Platforms: Services like Azure App Service, AWS Elastic Beanstalk, and Google App Engine offer managed hosting solutions.

Choosing the right deployment strategy involves considering your target environment, scalability needs, and operational expertise.