Configuration and options standard¶
This page sets the rules for configuration files and options classes in ConnectSoft services and libraries. It is written for engineers who add settings, options classes or host configuration, and for their reviewers. The options pattern and configuration sources are explained in Options Pattern and Configuration.
Secrets¶
Secrets never go into appsettings*.json files or any other committed file. CS-SEC-001 states the rule and the approved stores; it is not repeated here.
Rules¶
CS-CFG-001: Register options with validation on start¶
Severity: major · Applies to: service, library, template · Enforced by: manual · Status: active
Why¶
A missing or misspelt setting should stop the host at start-up with a clear message, not fail the first request that needs it. AddOptionsWithValidation from ConnectSoft.Extensions.Options binds the section strictly, so unknown keys are errors. It also runs DataAnnotations and the source-generated validator, and validates on start.
Do¶
- Declare a validator for each options class as a
partialclass marked[OptionsValidator]that implementsIValidateOptions<TOptions>. - Register the options with
services.AddOptionsWithValidation<TOptions, TValidator>(configuration, TOptions.SectionName). - Mark required settings with
[Required]and constrain values with[Range],[MinLength]and similar attributes. - Declare the section name as a
public const stringon the options class. - Give each options validator its own test class (CS-VAL-001).
Don't¶
- Bind options with
Configure<TOptions>(section)orBindwithout validation. - Read settings with
configuration["Key"]in application code.
Example¶
[OptionsValidator]
public partial class ValidateOrdersOptions : IValidateOptions<OrdersOptions>
{
}
services.AddOptionsWithValidation<OrdersOptions, ValidateOrdersOptions>(
configuration: configuration,
sectionName: OrdersOptions.OrdersOptionsSectionName);
How to check¶
- Every new options class in the change has an
[OptionsValidator]validator and is registered withAddOptionsWithValidation. - Search application code in the change for
configuration["reads.
CS-CFG-002: Inject IOptions<T> unless values must reload¶
Severity: minor · Applies to: service, library, template · Enforced by: manual · Status: active
Why¶
Most settings are fixed for the life of the process, and IOptions<T> reads them once, already validated. IOptionsMonitor<T> is needed only where a value must change without a restart. IOptionsSnapshot<T> is scoped, recomputes per request and cannot be used from singletons. The estate does not use it, and supporting it adds a third behaviour to reason about.
Do¶
- Inject
IOptions<T>by default. - Inject
IOptionsMonitor<T>only where the setting must reload at run time, and readCurrentValueat the point of use.
Don't¶
- Inject
IOptionsSnapshot<T>. - Copy
IOptionsMonitor<T>.CurrentValueinto a field in the constructor, which defeats the reload.
How to check¶
- Search the change for
IOptionsSnapshot<, and ask for eachIOptionsMonitor<why the value must reload. - A constructor that takes
IOptionsMonitor<T>does not storeCurrentValuein a field.
CS-CFG-003: Never give a bound collection property a default value¶
Severity: blocker · Applies to: service, library, template · Enforced by: manual (target: ENF-044) · Status: active
Why¶
The configuration binder adds the configured items to a collection that already has items, instead of replacing it. An options class whose array or list property has default items therefore holds the defaults plus the configured values. The binder creates the instance and runs field initializers whether or not the property is required. When the collection drives row creation, the duplicates break unique constraints or double the work.
Do¶
- Declare bound collection properties without an initializer, or with an empty one.
- Put the values in
appsettings.json, and enforce their presence with[Required]and[MinLength]. - Test binding with
configuration.GetSection(...).Get<T>(), which constructs the instance the way the options system does.
Don't¶
- Write
public string[] Keys { get; set; } = ["a", "b"];on a bound options class. - Test binding with
Bind(existingInstance)on an instance whose collections were cleared first; that test cannot fail.
Example¶
// Don't: binds to the two defaults plus every configured value.
public string[] SupportedCultures { get; set; } = ["en", "fr"];
// Do: configuration is the only source.
[Required]
[MinLength(1)]
public required string[] SupportedCultures { get; set; }
How to check¶
Search the options classes in the change for collection properties with a non-empty initializer.
CS-CFG-004: Keep base settings at Information level with console exporters off¶
Severity: major · Applies to: service, template · Enforced by: manual (target: ENF-043) · Status: active
Why¶
The base appsettings.json applies in every environment that does not override it. Synchronous sinks at Debug level add seconds to busy requests. The console trace exporter exports synchronously under a process-wide lock, which can stall unrelated requests completely. Both look like slow databases or network problems.
Do¶
- Set the default minimum log level in the base
appsettings.jsontoInformation. - Set
EnableConsoleExportertofalsein the base file. - Turn on Debug level or console export only in
appsettings.Development.jsonor a local file, and only while it is needed.
Don't¶
- Commit Debug or Verbose as the default level, or a console exporter, in a base or deployed settings file.
How to check¶
In each changed base appsettings.json, the default minimum level is Information and EnableConsoleExporter is false.
CS-CFG-005: Set environment-specific values in the deployment, not in committed environment files¶
Severity: minor · Applies to: service, pipelines, template · Enforced by: manual · Status: active
Why¶
The repositories ship base, Development, Docker and test settings files, and no appsettings.Production.json. Values that differ per deployed environment, such as addresses of other services, cluster ids and connection strings, are set by the deployment pipeline or by environment variables. A value left at its local default in a deployed environment points the service at the wrong place without an error.
Do¶
- Keep local and container defaults in the base, Development and Docker files.
- Set every value that differs per deployed environment in the deployment pipeline's settings or in environment variables.
- When you add such a setting, add it to the pipeline settings of every environment in the same change.
Don't¶
- Add environment files for deployed environments, such as
appsettings.Production.json, with addresses or connection strings.
How to check¶
- A new setting whose value differs per deployed environment has pipeline settings for each environment.
- The change adds no settings file for a deployed environment, such as
appsettings.Production.json, with addresses or connection strings.
CS-CFG-006: Open header propagation before middleware that calls other services¶
Severity: major · Applies to: service, template · Enforced by: manual · Status: active
Why¶
The outbound handler of Microsoft.AspNetCore.HeaderPropagation throws when the header propagation middleware has not run for the request. Middleware that calls another service through a client with header propagation, and that runs before UseHeaderPropagation(), therefore turns every request into a 500.
Do¶
- Register
UseHeaderPropagation()before any middleware that makes outbound calls through a client with header propagation. - Where the order is fixed by a shared package, open header propagation in the method that registers the middleware, immediately before it.
Don't¶
- Call another service from middleware without checking where header propagation is opened.
How to check¶
For new middleware that makes outbound calls, the pipeline order puts UseHeaderPropagation() first, and a test sends a request through the middleware.
CS-CFG-007: Add a required options section to every settings variant, guarded by a parity test¶
Severity: major · Applies to: service, template · Enforced by: manual · Status: active
Why¶
Hosts load settings differently: the application layers environment files over a base file, while an acceptance or test host may load one file alone. A required section added only to the base file makes every host that loads another variant fail at start.
Do¶
- Add a new required options section to every settings file that a host loads on its own.
- Keep a parity test that registers the options against each settings variant, loaded the way its host loads it.
Don't¶
- Add a section validated on start to the base settings file only.
How to check¶
Each new options section appears in every standalone settings variant, and the parity test covers it.
CS-CFG-008: Extend a registered options instance instead of registering it again¶
Severity: major · Applies to: service, library · Enforced by: manual · Status: active
Why¶
Library registration methods often add their options as a singleton instance with TryAdd. A second call from an extending project is ignored without an error, so its settings, such as extra assemblies to scan, never apply.
Do¶
- In a library, make the registration method safe to call more than once: configure the existing options instance or merge into it.
- In an extending project, pass its additions to the existing options, for example through a configure callback.
Don't¶
- Call a library's
Add...registration method a second time and expect the second call to win.
How to check¶
A test resolves the options through the real registration path and asserts that the extending project's additions are present.
CS-CFG-009: Restate every base array entry in an overlay settings file¶
Severity: major · Applies to: service, template · Enforced by: manual · Status: active
Why¶
The configuration system stores array entries as indexed keys. An overlay with fewer entries than the base replaces only the first slots, and the base's remaining entries stay active, such as a second log sink.
Do¶
- When an overlay changes an array, restate the whole array at the same length as the base, or move the array out of the base file.
- Cover the effective array of each environment with a test that builds the real layered configuration.
Don't¶
- Shorten a base array by writing fewer entries in an overlay.
How to check¶
Every overlay array in the change has the same number of entries as the base array at the same key path, and a test asserts the effective array for each environment.
CS-CFG-010: Declare every substituted key in the settings file it is substituted into¶
Severity: major · Applies to: service, template, pipelines · Enforced by: manual · Status: active
Why¶
JSON variable substitution in deployment only overwrites keys that already exist in the settings file. A key that one settings variant does not declare keeps the code default in that environment, and nothing reports it.
Do¶
- Add every key that a deployment substitutes to each settings variant of its section, with a placeholder value where the real value comes from the deployment.
- Keep a settings-shape test that compares the leaf keys of a section across all variants, acceptance-test variants included.
Don't¶
- Rely on a pipeline variable to create a key that no settings file declares.
How to check¶
Each key named in the deployment's substitution variables exists in every settings variant that the deployment transforms.
CS-CFG-011: Read a section only through its options type¶
Severity: minor · Applies to: service, library, template · Enforced by: manual · Status: active
Why¶
When a section has an options type, its defaults and its validation live there. Code that reads the same section through IConfiguration skips both, so a documented default or a start-up check has no effect on the value actually used.
Do¶
- Inject the options type and read the value from it.
- Add a property to the options type when code needs a key that the type does not have yet.
Don't¶
- Read
configuration.GetSection(...)["Key"]orGetValuefor a section that has an options type.
How to check¶
Search the change for GetSection( and GetValue< on section names that have an options type.
CS-CFG-012: Keep test and CI settings at Information level without console sinks¶
Severity: major · Applies to: service, template, pipelines · Enforced by: manual (target: ENF-067) · Status: active
Why¶
Test runners capture all console output and keep it in memory for the results file. Debug-level logging for the default or the ORM categories with a console sink can produce gigabytes in one run, which slows the run to tens of minutes and ends it with OutOfMemoryException in the results logger.
Do¶
- Set the default and ORM log levels in committed test and CI settings to
Informationor higher. - Raise a level for one category locally, in a settings file that is not committed, while you investigate.
- Apply this to each test settings file that a change adds or edits. Existing files that still set Debug are tracked as ENF-067 and change when they are next touched.
Don't¶
- Commit Debug or Trace levels, or a console sink, in test or CI settings.
How to check¶
Committed test settings files set no category below Information and enable no console sink.