ASP.NET Core wrong ASPNETCORE_ENVIRONMENT / config in CI
ASP.NET Core layers appsettings.{ASPNETCORE_ENVIRONMENT}.json on top of appsettings.json. If CI leaves the variable unset (defaults to Production) or sets an unexpected value, the app loads a different config than you expect and required settings go missing.
What this error means
Settings that exist in appsettings.Development.json are null in CI, or a feature behaves differently, because the effective environment (defaulting to Production) does not load the expected override file.
Unhandled exception. System.InvalidOperationException: Sequence contains no elements
(a required option bound from appsettings.Development.json was missing because
ASPNETCORE_ENVIRONMENT resolved to Production)Common causes
The variable is unset and defaults to Production
With no ASPNETCORE_ENVIRONMENT, the host uses Production and never loads appsettings.Development.json, so dev-only settings are absent.
The environment name does not match a file
A typo or an environment like CI has no matching appsettings.CI.json, so overrides you rely on are not applied.
How to fix it
Set the environment explicitly in CI
- Decide which environment CI should emulate and set
ASPNETCORE_ENVIRONMENTto it. - Ensure a matching
appsettings.{Environment}.jsonexists, or provide values via env vars. - Prefer environment variables for secrets over committed config files.
env:
ASPNETCORE_ENVIRONMENT: DevelopmentOverride individual settings via env vars
Configuration keys map to env vars with double underscores, so you can set them without an environment-specific file.
env:
Logging__LogLevel__Default: InformationHow to prevent it
- Set ASPNETCORE_ENVIRONMENT deliberately in CI rather than relying on the default.
- Keep an appsettings file for each environment name you use, or supply env vars.
- Do not depend on user-secrets or local files for values CI needs.