dotnet / dotnet/aspnetcore

Not all default services are added to WebApplicationBuilder.Services

Open
#34,825 4 comments 0 reactions 0 assignees View on GitHub
area-minimal feature-minimal-hosting Priority:3
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

HostBuilder adds some default services in [CreateServiceProvider()](https://github.com/dotnet/runtime/blob/e983168f871dcba090ff6ec08c0d2c7d4353fb8e/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs#L238-L260) that are not added to WebApplicationBuilder.Services until after build.

```csharp
var builder = WebApplication.CreateBuilder();
// System.InvalidOperationException: No service for type IHost has been registered.
builder.Services.BuildServiceProvider().GetRequiredService();
```

Before #34455 was merged, these services never showed up in WebApplicationBuilder.Services even after build. Because CreateServiceProvider() is called in HostBuilder.Build(), we cannot access these service descriptors any earlier without new public API.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.