Edit

Share via


HostingStartup is not supported with Aspire integrations

Aspire integrations require the use of IHostApplicationBuilder, but HostingStartup only provides access to IWebHostBuilder. This fundamental incompatibility means that you can't configure Aspire integrations from within a HostingStartup implementation.

Symptoms

When attempting to use Aspire integrations within a HostingStartup implementation, you might encounter:

  • Compilation errors: Aspire integration extension methods like AddNpgsqlDbContext or AddRedis are not available on IWebHostBuilder.
  • Runtime configuration issues: Even if you access the underlying services, the proper configuration and service registration won't occur.
  • Missing telemetry and resilience: Aspire's built-in observability, health checks, and resilience patterns won't be applied.

Why HostingStartup doesn't work with Aspire

Aspire integrations extend IHostApplicationBuilder to provide:

  • Standardized configuration patterns.
  • Built-in health checks.
  • Telemetry and observability.
  • Resilience patterns.
  • Service discovery integration.

The HostingStartup feature was designed for the older ASP.NET Core hosting model and only provides access to IWebHostBuilder, which doesn't include these modern hosting capabilities.

Migrating from HostingStartup

The HostingStartup feature represents an older ASP.NET Core hosting model that predates the modern IHostApplicationBuilder pattern that Aspire requires. Migration is necessary to leverage Aspire's integrations and modern hosting capabilities.

Understanding the API changes

The fundamental difference lies in the hosting abstractions:

Before (HostingStartup pattern):

using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

// MyDatabaseStartup.cs
public class MyDatabaseStartup : IHostingStartup
{
    public void Configure(IWebHostBuilder builder)
    {
        builder.ConfigureServices(services =>
        {
            // This won't work with Aspire integrations
            services.AddDbContext<MyDbContext>(options =>
                options.UseNpgsql("Host=localhost;Database=mydb;Username=user;Password=password"));
        });
    }
}

After (Modern hosting pattern):

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

// Example of modern hosting pattern that works with Aspire
public class ModernHostingExample
{
    public static void ConfigureServices()
    {
        // Program.cs equivalent
        var builder = WebApplication.CreateBuilder();

        // Add service defaults first
        // builder.AddServiceDefaults();

        // Now you can use Aspire integrations
        // builder.AddNpgsqlDbContext<MyDbContext>("postgres");

        var app = builder.Build();

        // app.MapDefaultEndpoints();
        // app.Run();
    }
}

Key conceptual changes

When migrating from HostingStartup to the modern hosting model, you're moving between these approaches:

Legacy pattern Modern pattern Benefit
IWebHostBuilder IHostApplicationBuilder Access to modern hosting features and Aspire integrations.
Separate startup classes Program.cs configuration Service configuration moves directly into the application's entry point for better clarity and debugging.
Manual service registration Integration packages Aspire integrations handle service registration, configuration, health checks, and telemetry automatically.

Migration resources

For detailed migration guidance, see:

Additional considerations

  • Service discovery: Aspire integrations automatically configure service discovery. If you were using HostingStartup for service-to-service communication, consider using Aspire's service discovery features.

  • Configuration management: Instead of hard-coding connection strings in HostingStartup, use Aspire's configuration patterns with connection string names that map to resources in your app host.

  • Testing: Aspire provides testing capabilities that work with the new hosting model.

For more information about Aspire integrations and the hosting model, see Aspire integrations overview.