|
| 1 | +--- |
| 2 | +title: HostingStartup is not supported with .NET Aspire integrations |
| 3 | +description: Learn how to migrate from HostingStartup to the IHostApplicationBuilder pattern for use with .NET Aspire integrations. |
| 4 | +ms.date: 08/04/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +--- |
| 7 | + |
| 8 | +# HostingStartup is not supported with .NET Aspire integrations |
| 9 | + |
| 10 | +.NET Aspire integrations require the use of <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder>, but `HostingStartup` only provides access to <xref:Microsoft.AspNetCore.Hosting.IWebHostBuilder>. This fundamental incompatibility means that you can't configure .NET Aspire integrations from within a `HostingStartup` implementation. |
| 11 | + |
| 12 | +## Symptoms |
| 13 | + |
| 14 | +When attempting to use .NET Aspire integrations within a HostingStartup implementation, you might encounter: |
| 15 | + |
| 16 | +- **Compilation errors**: Aspire integration extension methods like `AddNpgsqlDbContext` or `AddRedis` are not available on `IWebHostBuilder`. |
| 17 | +- **Runtime configuration issues**: Even if you access the underlying services, the proper configuration and service registration won't occur. |
| 18 | +- **Missing telemetry and resilience**: Aspire's built-in observability, health checks, and resilience patterns won't be applied. |
| 19 | + |
| 20 | +## Why HostingStartup doesn't work with .NET Aspire |
| 21 | + |
| 22 | +.NET Aspire integrations extend <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder> to provide: |
| 23 | + |
| 24 | +- Standardized configuration patterns. |
| 25 | +- Built-in health checks. |
| 26 | +- Telemetry and observability. |
| 27 | +- Resilience patterns. |
| 28 | +- Service discovery integration. |
| 29 | + |
| 30 | +The `HostingStartup` feature was designed for the older ASP.NET Core hosting model and only provides access to <xref:Microsoft.AspNetCore.Hosting.IWebHostBuilder>, which doesn't include these modern hosting capabilities. |
| 31 | + |
| 32 | +## Migrating from HostingStartup |
| 33 | + |
| 34 | +The `HostingStartup` feature represents an older ASP.NET Core hosting model that predates the modern <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder> pattern that .NET Aspire requires. Migration is necessary to leverage .NET Aspire's integrations and modern hosting capabilities. |
| 35 | + |
| 36 | +### Understanding the API changes |
| 37 | + |
| 38 | +The fundamental difference lies in the hosting abstractions: |
| 39 | + |
| 40 | +**Before (HostingStartup pattern):** |
| 41 | + |
| 42 | +:::code language="csharp" source="snippets/hosting-startup-not-supported/hosting-startup-before.cs"::: |
| 43 | + |
| 44 | +**After (Modern hosting pattern):** |
| 45 | + |
| 46 | +:::code language="csharp" source="snippets/hosting-startup-not-supported/host-application-builder-after.cs"::: |
| 47 | + |
| 48 | +### Key conceptual changes |
| 49 | + |
| 50 | +When migrating from `HostingStartup` to the modern hosting model, you're moving between these approaches: |
| 51 | + |
| 52 | +| Legacy pattern | Modern pattern | Benefit | |
| 53 | +|---|---|---| |
| 54 | +| `IWebHostBuilder` | `IHostApplicationBuilder` | Access to modern hosting features and .NET Aspire integrations. | |
| 55 | +| Separate startup classes | Program.cs configuration | Service configuration moves directly into the application's entry point for better clarity and debugging. | |
| 56 | +| Manual service registration | Integration packages | .NET Aspire integrations handle service registration, configuration, health checks, and telemetry automatically. | |
| 57 | + |
| 58 | +### Migration resources |
| 59 | + |
| 60 | +For detailed migration guidance, see: |
| 61 | + |
| 62 | +- [Migrate from ASP.NET Core 5.0 to 6.0](/aspnet/core/migration/50-to-60?view=aspnetcore-9.0) - Covers the transition to the minimal hosting model |
| 63 | +- [David Fowl's ASP.NET Core 6.0 migration guide](https://gist.github.com/davidfowl/0e0372c3c1d895c3ce195ba983b1e03d) - Provides practical migration patterns and examples |
| 64 | + |
| 65 | +## Additional considerations |
| 66 | + |
| 67 | +- **Service discovery**: .NET Aspire integrations automatically configure service discovery. If you were using HostingStartup for service-to-service communication, consider using Aspire's [service discovery features](../service-discovery/overview.md). |
| 68 | + |
| 69 | +- **Configuration management**: Instead of hard-coding connection strings in HostingStartup, use .NET Aspire's configuration patterns with connection string names that map to resources in your app host. |
| 70 | + |
| 71 | +- **Testing**: .NET Aspire provides [testing capabilities](../testing/overview.md) that work with the new hosting model. |
| 72 | + |
| 73 | +For more information about .NET Aspire integrations and the hosting model, see [.NET Aspire integrations overview](../fundamentals/integrations-overview.md). |
0 commit comments