Skip to content

Fix wrong order for default configuration #46538

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rufer7
Copy link

@rufer7 rufer7 commented Jun 3, 2025

Summary

This pull request includes a minor reordering of the configuration providers list in the docs/core/extensions/configuration.md file. The change ensures that the appsettings.json configuration provider is listed after the appsettings.*Environment*.json provider for better clarity and alignment with the intended configuration precedence.

Key change:

  • docs/core/extensions/configuration.md: Reordered the appsettings.json entry to follow the appsettings.*Environment*.json entry in the list of configuration providers. This change ensures the order reflects the configuration precedence more accurately.

Internal previews

📄 File 🔗 Preview link
docs/core/extensions/configuration.md Configuration in .NET

@rufer7 rufer7 requested review from IEvangelist and a team as code owners June 3, 2025 06:50
@dotnetrepoman dotnetrepoman bot added this to the June 2025 milestone Jun 3, 2025
@dotnet-policy-service dotnet-policy-service bot added dotnet-fundamentals/svc community-contribution Indicates PR is created by someone from the .NET community. labels Jun 3, 2025
@IEvangelist
Copy link
Member

This is inaccurate. The order is correct as documented. See the source code, the order a provider is added is the order:

https://github.com/dotnet/runtime/blob/d221687c3724d26d653d022f4b254bc1d7eb1a6b/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs#L242-L243

@IEvangelist IEvangelist closed this Jul 8, 2025
@rufer7
Copy link
Author

rufer7 commented Jul 8, 2025

@IEvangelist hmm... I still think my suggested change is accurate. Let me please explain why.

The documentation states the following right above the list.

The <xref:Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder(System.String[])?displayProperty=nameWithType> method provides default configuration for the app in the following order, from highest to lowest priority:

So based on the current version of the docs, appsettings.json would take precedence over appsettings.Environment.json which is wrong based on the link you pasted as .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: reloadOnChange); is added AFTER .AddJsonFile("appsettings.json", optional: true, reloadOnChange: reloadOnChange).

Furthermore, in the docs there is the following example right after the list which would match the suggested change as well as the expected behavior.

Adding a configuration provider overrides previous configuration values. For example, the [Command-line configuration provider](configuration-providers.md#command-line-configuration-provider) overrides all values from other providers because it's added last. If `SomeKey` is set in both *appsettings.json* and the environment, the environment value is used because it was added after *appsettings.json*.

@DarioErismann
Copy link

@IEvangelist I completely agree with @rufer7. According to the source code you shared, appsettings.json is added BEFORE appsettings.Environment.json.
This means that appsettings.json has the LOWER priority.

@rufer7
Copy link
Author

rufer7 commented Jul 14, 2025

@IEvangelist can you please revisit this PR?

@gewarren gewarren reopened this Jul 15, 2025
@dotnetrepoman dotnetrepoman bot modified the milestones: June 2025, July 2025 Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community. dotnet-fundamentals/svc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants