Skip to content

Conversation

@halter73
Copy link
Member

Fixes #33934

@halter73 halter73 requested a review from Tratcher as a code owner July 28, 2021 00:16
@ghost ghost added the area-runtime label Jul 28, 2021
@halter73 halter73 changed the title Only add default services once Only add default services once in WebApplicationBuilder Jul 28, 2021
@halter73 halter73 requested a review from davidfowl July 28, 2021 00:36
[Fact]
public void WebApplicationBuilder_EnablesServiceScopeValidationByDefaultInDevelopment()
{
// The environment cannot be reconfigured after the builder is created currently.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not part of this PR but we should talk about ways to throw if UseEnvironment is used after this call (and maybe we need a first class API).

CopyPropertiesTo(context.HostingEnvironment);
});
var context = (WebHostBuilderContext)hostBuilder.Properties[typeof(WebHostBuilderContext)];
CopyPropertiesTo(context.HostingEnvironment);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you even need to do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not, we get test failures here and other similar places since WebApplication.Environment pulls from the IServiceProvider and we no longer register our IWebHostEnvironment as a service.

@davidfowl
Copy link
Member

I debugged the issue, the IWebHostEnvironment.ApplicationName is incorrect and it results in not being able to find controllers or views.

}

_environment.ApplyConfigurationSettings(_configuration);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we ApplyConfigurationSettings twice when nothing is observing changes to _environment between these 2 calls.?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't new. ConfigureHostConfiguration callbacks to read environment variables (e.g. DOTNET_ and ASPNETCORE_) that need to be reacted to before running ConfigureAppConfiguration callbacks to read config that's dependent on the environment name configured by the environment variables (e.g. appsettings.{env.EnvironmentName}.json and user secrets).

This is the last call to ApplyConfigurationSettings before the WebApplicationBuilder ctor exits, so it's to ensure app code in Program.cs can observe any changes made to the environment in appsettings mostly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After playing with this a bit I think we only need to read hosting configuration once at the beginning and blocking it after that. We should also get rid of our custom IWebHostEnvironment and should use the one configured in the IHostBuilder.Properties[typeof(WebHostContext)]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

Comment on lines +137 to +141
// Ideally, we'd only call _hostBuilder.ConfigureWebHost(ConfigureWebHost) instead of
// _hostBuilder.ConfigureWebHostDefaults(ConfigureWebHost) to avoid some duplicate service descriptors,
// but we want to add services in the WebApplicationBuilder constructor so code can inspect
// WebApplicationBuilder.Services. At the same time, we want to be able which services are loaded
// to react to config changes (e.g. ForwardedHeadersStartupFilter).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Ideally, we'd only call _hostBuilder.ConfigureWebHost(ConfigureWebHost) instead of
// _hostBuilder.ConfigureWebHostDefaults(ConfigureWebHost) to avoid some duplicate service descriptors,
// but we want to add services in the WebApplicationBuilder constructor so code can inspect
// WebApplicationBuilder.Services. At the same time, we want to be able which services are loaded
// to react to config changes (e.g. ForwardedHeadersStartupFilter).

@davidfowl
Copy link
Member

This PR gave me clarity on how we should fix the remaining issues with the WebApplicationBuilder.

@davidfowl davidfowl merged commit 18a9268 into main Jul 28, 2021
@davidfowl davidfowl deleted the halter73/33934 branch July 28, 2021 09:23
@ghost ghost added this to the 6.0-rc1 milestone Jul 28, 2021
@amcasey amcasey added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-runtime labels Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-hosting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebApplicationBuilder adds default services twice

4 participants