Skip to content

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

Merged
merged 13 commits into from
Jun 26, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 24, 2025

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

  • Modified ApplyDefaultAppConfiguration in HostingHostBuilderExtensions.cs to load application-specific configuration files after existing appsettings.json files
  • Files are loaded in the correct precedence order:
    1. appsettings.json
    2. appsettings.[EnvironmentName].json
    3. [ApplicationName].settings.json (new)
    4. [ApplicationName].settings.[EnvironmentName].json (new)

Special Case Handling

  • Conditional Loading: Only loads application-specific files when ApplicationName is not null or empty
  • Path Sanitization: Replaces path separator characters (/ and \) with underscores (_) in ApplicationName to ensure valid file names
  • Optional Files: All new configuration files are optional and support reloadOnChange

Documentation Updates

  • Updated XML documentation in HostingHostBuilderExtensions.cs and HostApplicationBuilder.cs to reflect the new behavior
  • Added clear descriptions of when and how application-specific settings are loaded

Comprehensive Testing

Added 4 new tests covering:

  • Basic loading of application-specific settings with correct precedence
  • Skipping when ApplicationName is empty
  • Path separator replacement functionality
  • Override behavior (app-specific settings override general appsettings)

Example Usage

For a file-based app like app.cs, the host will now attempt to load:

appsettings.json                    # Existing behavior
appsettings.Development.json        # Existing behavior  
app.settings.json                   # New - app-specific settings
app.settings.Development.json       # New - app-specific environment settings

For a project like MyApp.csproj, it will load:

appsettings.json
appsettings.Production.json
MyApp.settings.json
MyApp.settings.Production.json

Test Results

  • All existing tests continue to pass (251 → 255 total tests)
  • Manual testing confirms correct loading order and precedence
  • No breaking changes to existing functionality

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.

@Copilot Copilot AI changed the title [WIP] Add loading of [ApplicationName].settings.json & [ApplicationName].settings.[EnvironmentName].json to default Host Add support for application-specific settings files in Host defaults Jun 24, 2025
@Copilot Copilot AI requested a review from DamianEdwards June 24, 2025 21:17
Copilot finished work on behalf of DamianEdwards June 24, 2025 21:17
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-hosting
See info in area-owners.md if you want to be subscribed.

@DamianEdwards DamianEdwards marked this pull request as ready for review June 25, 2025 00:37
@Copilot Copilot AI review requested due to automatic review settings June 25, 2025 00:37
Copy link
Contributor

@Copilot Copilot AI left a 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.

Copilot finished work on behalf of eerhardt June 25, 2025 16:03
@Copilot Copilot AI requested a review from eerhardt June 25, 2025 16:03
@Copilot Copilot AI requested a review from DamianEdwards June 25, 2025 22:17
Copilot finished work on behalf of DamianEdwards June 25, 2025 22:17
@Copilot Copilot AI requested a review from DamianEdwards June 25, 2025 22:45
Copilot finished work on behalf of DamianEdwards June 25, 2025 22:45
@Copilot Copilot AI requested a review from DamianEdwards June 25, 2025 23:09
Copilot finished work on behalf of DamianEdwards June 25, 2025 23:09
Copilot finished work on behalf of DamianEdwards June 25, 2025 23:33
// 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
Copy link
Member

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.:

Suggested change
// 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

Copy link
Member

@captainsafia captainsafia left a 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?

@DamianEdwards
Copy link
Member

Nit: formatting seems a bit off on the chained calls?

Yeah we really need to get dotnet format automated as part of agent PRs.

@DamianEdwards DamianEdwards merged commit 7f5a5fd into main Jun 26, 2025
81 of 87 checks passed
@BrennanConroy BrennanConroy deleted the copilot/fix-114302 branch June 26, 2025 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add loading of [ApplicationName].settings.json & [ApplicationName].settings.[EnvironmentName].json to default Host
6 participants