Where to Find the ASP.NET Core Hosting Source Code: A Complete Guide to the dotnet/aspnetcore Repository

The ASP.NET Core hosting source code resides in the src/Hosting/Hosting/src directory of the dotnet/aspnetcore repository, implementing the hosting stack through WebHostBuilder, WebHost, and GenericWebHostBuilder classes.

If you are looking to understand how ASP.NET Core initializes, configures, and runs web applications, examining the ASP.NET Core hosting source code is essential. The dotnet/aspnetcore repository contains the complete implementation of the hosting layer, responsible for building hosts, managing application lifetimes, and coordinating server startup. This guide maps the specific source files and classes that power both the classic WebHost model and the modern generic host approach.

Core Hosting Components in the Source Code

WebHostBuilder – The Classic Entry Point

The WebHostBuilder class in src/Hosting/Hosting/src/WebHostBuilder.cs provides the original builder API for constructing IWebHost instances. It parses configuration, wires up the server, discovers the Startup class, and creates the concrete host. While still available for backward compatibility, this represents the pre-generic host model.

WebHost – Concrete Host Implementation

Located at src/Hosting/Hosting/src/Internal/WebHost.cs, the WebHost class holds the IServiceProvider, IWebHostEnvironment, and the selected IServer implementation. It manages the actual startup sequence, graceful shutdown mechanics, and diagnostic startup logs.

The GenericWebHostBuilder class in src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs bridges the generic host (Host.CreateDefaultBuilder) to the web-specific pipeline. This is the recommended approach in modern ASP.NET Core applications, providing a unified host capable of running web, console, or background services.

Startup Loading and Reflection

The StartupLoader class in src/Hosting/Hosting/src/Internal/StartupLoader.cs uses reflection to locate and invoke the user's Configure and ConfigureServices methods. It handles the discovery of the Startup type and executes the configuration methods via StartupMethods.

Server Address Management

The ServerAddressesFeature class in src/Hosting/Hosting/src/Server/Features/ServerAddressesFeature.cs implements IServerAddressesFeature, exposing the ports and addresses the server is actively listening on at runtime.

Application Lifetime and Shutdown Coordination

The WebHostLifetime class in src/Hosting/Hosting/src/Internal/WebHostLifetime.cs coordinates application lifetime events including ApplicationStarted, ApplicationStopping, and ApplicationStopped, tying the host's lifecycle to the server's shutdown process.

Static Web Assets and Extension Points

Beyond the core hosting logic, the repository contains additional components for static assets and configuration extensions.

Static Web Assets Loader

The StaticWebAssetsLoader class in src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs builds a composite file provider that aggregates static assets from referenced projects, enabling the serving of static files across project boundaries.

Builder Extension Methods

Utility classes WebHostBuilderExtensions (src/Hosting/Hosting/src/WebHostBuilderExtensions.cs) and WebHostExtensions (src/Hosting/Hosting/src/WebHostExtensions.cs) provide public APIs for customizing host configuration through fluent extension methods.

Understanding the ASP.NET Core hosting source code requires familiarity with these specific implementation files:

  • WebHostBuilder.cs – Entry point for the classic host builder; parses configuration and wires up dependencies.
  • WebHost.cs – Concrete host implementation managing the service provider, environment, and server lifecycle.
  • GenericWebHostBuilder.cs – Adapter that enables the generic host model to support web applications.
  • StartupLoader.cs – Reflection-based discovery and execution of user-defined Startup classes.
  • WebHostLifetime.cs – Manages application lifetime events and graceful shutdown coordination.
  • ServerAddressesFeature.cs – Exposes active listening addresses through the server features collection.
  • StaticWebAssetsLoader.cs – Discovers and aggregates static web assets from project references.
  • WebHostBuilderExtensions.cs – Public extension methods for customizing WebHostBuilder configuration.

Practical Code Examples

The following examples demonstrate how the source code components interact in real-world scenarios.

Classic WebHost Configuration

This pattern uses the WebHostBuilder class directly, as implemented in src/Hosting/Hosting/src/WebHostBuilder.cs:

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

public class Program
{
    public static void Main(string[] args)
    {
        new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build()
            .Run();
    }
}

Generic Host with Web Support

The recommended approach uses GenericWebHostBuilder internally via ConfigureWebHostDefaults:

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

var host = Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseStartup<Startup>();   // Uses GenericWebHostBuilder internally
    })
    .Build();

await host.RunAsync();

Inspecting Server Addresses at Runtime

Access the IServerAddressesFeature implementation from ServerAddressesFeature.cs:

using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Hosting;

var host = Host.CreateDefaultBuilder(args)
               .ConfigureWebHostDefaults(web => web.UseStartup<Startup>())
               .Build();

host.Start();

var addresses = host.Services.GetRequiredService<IServer>()
                .Features.Get<IServerAddressesFeature>();

foreach (var addr in addresses.Addresses)
{
    Console.WriteLine($"Listening on {addr}");
}

Summary

  • The ASP.NET Core hosting source code is located in src/Hosting/Hosting/src within the dotnet/aspnetcore repository.
  • WebHostBuilder.cs provides the classic builder API, while GenericWebHostBuilder.cs enables the modern generic host model.
  • WebHost.cs contains the concrete implementation managing service providers, environment, and server lifecycle.
  • StartupLoader.cs handles reflection-based discovery of configuration methods.
  • WebHostLifetime.cs coordinates application lifetime events and graceful shutdown.
  • ServerAddressesFeature.cs exposes runtime listening addresses.
  • The generic host approach (Host.CreateDefaultBuilder) is the recommended pattern, internally utilizing the same underlying components as the classic model.

Frequently Asked Questions

Where exactly is the ASP.NET Core hosting source code located?

The hosting implementation resides in the src/Hosting/Hosting/src directory of the dotnet/aspnetcore GitHub repository. This directory contains the builder classes, host implementations, and lifetime management code that initialize and run ASP.NET Core applications.

What is the difference between WebHostBuilder and GenericWebHostBuilder?

WebHostBuilder (in WebHostBuilder.cs) represents the classic pre-3.0 hosting model that builds IWebHost instances directly. GenericWebHostBuilder (in GenericHost/GenericWebHostBuilder.cs) adapts the generic host (IHost) to support web applications, providing a unified hosting model for web, console, and background services. The generic host approach is now the recommended pattern.

How does ASP.NET Core discover and load the Startup class?

The framework uses StartupLoader (in Internal/StartupLoader.cs) to locate the user's Startup type through reflection. It then invokes the ConfigureServices and Configure methods using the StartupMethods class, wiring up the dependency injection container and middleware pipeline according to the source code implementation.

How can I access the server's listening addresses at runtime?

The ServerAddressesFeature class (in Server/Features/ServerAddressesFeature.cs) implements IServerAddressesFeature, which exposes the active endpoints through the server's feature collection. Access this via host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>() after the host has started.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →