Skip to content

Add loading of [ApplicationName].settings.json & [ApplicationName].settings.[EnvironmentName].json to default Host #114302

@DamianEdwards

Description

@DamianEdwards

Sibling issue for ASP.NET Core: dotnet/aspnetcore#61343

To better support the upcoming file-based apps scenario coming in .NET 10 with dotnet run app.cs, the Host defaults should be updated to support loading more JSON configuration files based on the application name (and environment name). This allows for multiple file-based apps to be located in the same directory and have separate configuration files. The existing defaults mean that shared configuration can be placed in the files using the existing naming scheme, i.e. appsettings.json.

For example, a file-based app like app.cs should by default attempt to load JSON configuration files from files named app.settings.json and app.settings.[EnvironmentName].json, (e.g. app.settings.Development.json). Additionally, project-based apps would get the same new behavior, so that a project like MyApp.csproj would by default attempt to load JSON configuration files from files named MyApp.settings.json and MyApp.settings.[EnvironmentName].json. In both cases, these files should be loaded after and in addition to the existing JSON file names that are loaded by the Host defaults.

The code in question is at

appConfigBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: reloadOnChange)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: reloadOnChange);

The new code to add after these lines would be something like:

config.AddJsonFile($"{env.ApplicationName}.settings.json", optional: true, reloadOnChange: true)
      .AddJsonFile($"{env.ApplicationName}.settings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

Alternatives

The naming scheme proposed uses settings as the common term indicating what the file is for. This was changed from the existing appsettings simply to make it a bit smaller as doing so arguably doesn't meaningfully change the understandability. We could take this opportunity to choose a different term though.

config might better be an interesting choice so as to align with the fact that the entries in this file are loaded by the configuration APIs, e.g. app.config.json. When coupled with dotnet/sdk#48200 that would yield the following files:

- myapp/
  - app.cs
  - app.config.json
  - app.run.json

Of course we could instead continue to use the established appsettings term, resulting in:

- myapp/
  - app.cs
  - app.appsettings.json
  - app.run.json

Metadata

Metadata

Labels

area-Extensions-HostingenhancementProduct code improvement that does NOT require public API changes/additions

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions