-
Notifications
You must be signed in to change notification settings - Fork 155
Document HostingStartup incompatibility and migration to modern hosting patterns for .NET Aspire #4187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/fix-3861
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−0
Open
Document HostingStartup incompatibility and migration to modern hosting patterns for .NET Aspire #4187
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8c918c
Initial plan
Copilot c1e99bd
Add troubleshooting documentation for HostingStartup incompatibility
Copilot aa3a496
Fix duplicate link and complete documentation
Copilot 277c15c
Fix style guide compliance: remove package-lock.json, add trailing ne…
Copilot 7b9a6ef
Remove Option 4 and dependency injection plugin example code
Copilot 329b9fc
Refactor troubleshooting doc to focus on migration concepts rather th…
Copilot d057482
Fix bullet points to be complete sentences with proper punctuation
Copilot d499a6f
Update docs/troubleshooting/hosting-startup-not-supported.md
IEvangelist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
title: HostingStartup is not supported with .NET Aspire integrations | ||
description: Learn how to migrate from HostingStartup to the IHostApplicationBuilder pattern for use with .NET Aspire integrations. | ||
ms.date: 08/04/2025 | ||
ai-usage: ai-assisted | ||
--- | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# HostingStartup is not supported with .NET Aspire integrations | ||
|
||
.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. | ||
|
||
## Symptoms | ||
|
||
When attempting to use .NET 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 .NET Aspire | ||
|
||
.NET Aspire integrations extend <xref:Microsoft.Extensions.Hosting.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 <xref:Microsoft.AspNetCore.Hosting.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 <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder> pattern that .NET Aspire requires. Migration is necessary to leverage .NET Aspire's integrations and modern hosting capabilities. | ||
|
||
### Understanding the API changes | ||
|
||
The fundamental difference lies in the hosting abstractions: | ||
|
||
**Before (HostingStartup pattern):** | ||
|
||
:::code language="csharp" source="snippets/hosting-startup-not-supported/hosting-startup-before.cs"::: | ||
|
||
**After (Modern hosting pattern):** | ||
|
||
:::code language="csharp" source="snippets/hosting-startup-not-supported/host-application-builder-after.cs"::: | ||
|
||
### Key conceptual changes | ||
|
||
When migrating from `HostingStartup` to the modern hosting model, you're moving from: | ||
|
||
- **`IWebHostBuilder`** → **`IHostApplicationBuilder`**: The new builder provides access to modern hosting features and .NET 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**: .NET Aspire integrations handle service registration, configuration, health checks, and telemetry automatically. | ||
|
||
### Migration resources | ||
|
||
For detailed migration guidance, see: | ||
|
||
- [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 | ||
- [David Fowl's ASP.NET Core 6.0 migration guide](https://gist.github.com/davidfowl/0e0372c3c1d895c3ce195ba983b1e03d) - Provides practical migration patterns and examples | ||
|
||
## Additional considerations | ||
|
||
- **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). | ||
|
||
- **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. | ||
|
||
- **Testing**: .NET Aspire provides [testing capabilities](../testing/overview.md) that work with the new hosting model. | ||
|
||
For more information about .NET Aspire integrations and the hosting model, see [.NET Aspire integrations overview](../fundamentals/integrations-overview.md). |
13 changes: 13 additions & 0 deletions
13
.../troubleshooting/snippets/hosting-startup-not-supported/host-application-builder-after.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Program.cs | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add service defaults first | ||
builder.AddServiceDefaults(); | ||
|
||
// Now you can use Aspire integrations | ||
builder.AddNpgsqlDbContext<MyDbContext>("postgres"); | ||
|
||
var app = builder.Build(); | ||
|
||
app.MapDefaultEndpoints(); | ||
app.Run(); |
13 changes: 13 additions & 0 deletions
13
docs/troubleshooting/snippets/hosting-startup-not-supported/hosting-startup-before.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// MyDatabaseStartup.cs | ||
public class MyDatabaseStartup : IHostingStartup | ||
Check failure on line 2 in docs/troubleshooting/snippets/hosting-startup-not-supported/hosting-startup-before.cs
|
||
{ | ||
public void Configure(IWebHostBuilder builder) | ||
Check failure on line 4 in docs/troubleshooting/snippets/hosting-startup-not-supported/hosting-startup-before.cs
|
||
{ | ||
builder.ConfigureServices(services => | ||
{ | ||
// This won't work with Aspire integrations | ||
services.AddDbContext<MyDbContext>(options => | ||
options.UseNpgsql(connectionString)); | ||
}); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...oubleshooting/snippets/hosting-startup-not-supported/hosting-startup-not-supported.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.