-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Add support for application-specific settings files in Host defaults #116987
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
Conversation
Co-authored-by: DamianEdwards <[email protected]>
Tagging subscribers to this area: @dotnet/area-extensions-hosting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces application-specific configuration file loading to the default Host configuration, enabling settings files named according to the application name and environment.
- Added conditional logic in the host builder to load "[ApplicationName].settings.json" and "[ApplicationName].settings.[EnvironmentName].json" when an application name is provided.
- Enhanced path sanitization for ApplicationName and updated XML documentation.
- Implemented four new unit tests to validate the new behavior and its edge cases.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs | New unit tests covering app-specific settings loading, path separator sanitization, non-loading when ApplicationName is empty, and override behavior. |
src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs | Updated logic to load application-specific JSON configuration files based on ApplicationName with proper file name sanitization. |
src/libraries/Microsoft.Extensions.Hosting/src/HostApplicationBuilder.cs | Documentation updates to reflect the new configuration loading functionality. |
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs
Outdated
Show resolved
Hide resolved
…eanup Co-authored-by: eerhardt <[email protected]>
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs
Show resolved
Hide resolved
…mbly names Co-authored-by: DamianEdwards <[email protected]>
src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
…rvation Co-authored-by: DamianEdwards <[email protected]>
src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
…r clarity Co-authored-by: DamianEdwards <[email protected]>
…serve original behavior Co-authored-by: DamianEdwards <[email protected]>
// Verify that ApplicationName retains original value (not sanitized) | ||
Assert.Equal("my/app", hostEnvironment.ApplicationName); | ||
|
||
// Verify that path separators are replaced with underscores for file loading |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment could be better, e.g.:
// Verify that path separators are replaced with underscores for file loading | |
// Verify the value from the application specific settings file with the sanitized name is loaded |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Nit: formatting seems a bit off on the chained calls?
Yeah we really need to get |
This PR implements the feature requested in issue #114302 to add loading of
[ApplicationName].settings.json
and[ApplicationName].settings.[EnvironmentName].json
configuration files to the default Host configuration.Changes Made
Core Implementation
ApplyDefaultAppConfiguration
inHostingHostBuilderExtensions.cs
to load application-specific configuration files after existing appsettings.json filesappsettings.json
appsettings.[EnvironmentName].json
[ApplicationName].settings.json
(new)[ApplicationName].settings.[EnvironmentName].json
(new)Special Case Handling
ApplicationName
is not null or empty/
and\
) with underscores (_
) in ApplicationName to ensure valid file namesreloadOnChange
Documentation Updates
HostingHostBuilderExtensions.cs
andHostApplicationBuilder.cs
to reflect the new behaviorComprehensive Testing
Added 4 new tests covering:
Example Usage
For a file-based app like
app.cs
, the host will now attempt to load:For a project like
MyApp.csproj
, it will load:Test Results
This enables multiple file-based apps in the same directory to have separate configuration files while maintaining shared configuration in the existing appsettings.json files.
Fixes #114302.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.