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

> Discover the ASP.NET Core hosting source code in the dotnet/aspnetcore repository. Explore the src/Hosting/Hosting/src directory to understand its hosting stack.

- Repository: [.NET Platform/aspnetcore](https://github.com/dotnet/aspnetcore)
- Tags: how-to-guide
- Published: 2026-07-13

---

**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`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/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.

### GenericWebHostBuilder – The Modern Recommended Approach

The `GenericWebHostBuilder` class in [`src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/src/Hosting/Hosting/src/WebHostBuilderExtensions.cs)) and `WebHostExtensions` ([`src/Hosting/Hosting/src/WebHostExtensions.cs`](https://github.com/dotnet/aspnetcore/blob/main/src/Hosting/Hosting/src/WebHostExtensions.cs)) provide public APIs for customizing host configuration through fluent extension methods.

## Navigating the Source Code: Key Files and Responsibilities

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

- **[`WebHostBuilder.cs`](https://github.com/dotnet/aspnetcore/blob/main/WebHostBuilder.cs)** – Entry point for the classic host builder; parses configuration and wires up dependencies.
- **[`WebHost.cs`](https://github.com/dotnet/aspnetcore/blob/main/WebHost.cs)** – Concrete host implementation managing the service provider, environment, and server lifecycle.
- **[`GenericWebHostBuilder.cs`](https://github.com/dotnet/aspnetcore/blob/main/GenericWebHostBuilder.cs)** – Adapter that enables the generic host model to support web applications.
- **[`StartupLoader.cs`](https://github.com/dotnet/aspnetcore/blob/main/StartupLoader.cs)** – Reflection-based discovery and execution of user-defined Startup classes.
- **[`WebHostLifetime.cs`](https://github.com/dotnet/aspnetcore/blob/main/WebHostLifetime.cs)** – Manages application lifetime events and graceful shutdown coordination.
- **[`ServerAddressesFeature.cs`](https://github.com/dotnet/aspnetcore/blob/main/ServerAddressesFeature.cs)** – Exposes active listening addresses through the server features collection.
- **[`StaticWebAssetsLoader.cs`](https://github.com/dotnet/aspnetcore/blob/main/StaticWebAssetsLoader.cs)** – Discovers and aggregates static web assets from project references.
- **[`WebHostBuilderExtensions.cs`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/src/Hosting/Hosting/src/WebHostBuilder.cs):

```csharp
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`:

```csharp
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`](https://github.com/dotnet/aspnetcore/blob/main/ServerAddressesFeature.cs):

```csharp
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`](https://github.com/dotnet/aspnetcore/blob/main/WebHostBuilder.cs)** provides the classic builder API, while **[`GenericWebHostBuilder.cs`](https://github.com/dotnet/aspnetcore/blob/main/GenericWebHostBuilder.cs)** enables the modern generic host model.
- **[`WebHost.cs`](https://github.com/dotnet/aspnetcore/blob/main/WebHost.cs)** contains the concrete implementation managing service providers, environment, and server lifecycle.
- **[`StartupLoader.cs`](https://github.com/dotnet/aspnetcore/blob/main/StartupLoader.cs)** handles reflection-based discovery of configuration methods.
- **[`WebHostLifetime.cs`](https://github.com/dotnet/aspnetcore/blob/main/WebHostLifetime.cs)** coordinates application lifetime events and graceful shutdown.
- **[`ServerAddressesFeature.cs`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/WebHostBuilder.cs)) represents the classic pre-3.0 hosting model that builds `IWebHost` instances directly. `GenericWebHostBuilder` (in [`GenericHost/GenericWebHostBuilder.cs`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/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.