Skip to content
Latchkey

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.

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

  1. Decide which environment CI should emulate and set ASPNETCORE_ENVIRONMENT to it.
  2. Ensure a matching appsettings.{Environment}.json exists, or provide values via env vars.
  3. Prefer environment variables for secrets over committed config files.
.github/workflows/ci.yml
env:
  ASPNETCORE_ENVIRONMENT: Development

Override individual settings via env vars

Configuration keys map to env vars with double underscores, so you can set them without an environment-specific file.

.github/workflows/ci.yml
env:
  Logging__LogLevel__Default: Information

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

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →