Service provider validation hides actual exception while adding services in ConfigureServices
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
Hello
The following behavior happens **only when application is hosted in OutOfProcess** model:
We use the UseDefaultServiceProvider extension for the hostBuilder to set Service provider validation based on environment.
Basically, host building looks like this:
```
private static IHostBuilder CreateWebHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
})
.UseSerilog()
.UseDefaultServiceProvider((context, options) =>
{
if (
context.HostingEnvironment.IsDevelopmentLocal() ||
context.HostingEnvironment.IsDevelopment())
{
options.ValidateScopes = true;
options.ValidateOnBuild = true;
}
})
.ConfigureAppConfiguration(builder =>
{
//code omitted
});
}
```
If there is an exception while building the service provider in ConfigureServices(IServiceCollection) in Startup, it gets hidden because after that the runtime still calls the action provided to UseDefaultServiceProvider as if there was no exception.
As a result I get only one rich AggregateException containing multiple exceptions from the validation "Error while validating the service descriptor ... Unable to resolve service for type ...". Yes, the service provider cannot resolve this thing, but I want to see the initial exception that caused the normal build to be interrupted.
If the application is hosted with InProcess model the action for UseDefaultServiceProvider is never called and I can see the concrete exception.
Setup:
Target framework: **.netcore 3.1**
Visual Studio 2019 Version 16.8.4
.NET SDK (reflecting any global.json):
Version: 5.0.102
Commit: 71365b4d42
Runtime Environment:
OS Name: Windows
OS Version: 10.0.14393
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.102\
Host (useful for support):
Version: 5.0.2
Commit: cb5f173b96
Thank you
Contributor guide
Research direction
Start by reproducing the behavior in ConfigureServices(IServiceCollection) with UseDefaultServiceProvider and compare OutOfProcess with InProcess hosting. Trace when the service-provider options action runs after a build exception; done means the original exception remains visible instead of being obscured by validation errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100