Skip to content
Latchkey

System.TypeInitializationException in tests in CI

TypeInitializationException wraps an exception thrown by a type's static constructor (or static field initializer). The real cause is the inner exception - commonly a missing connection string, environment variable, or file that is present locally but absent in CI when the static initializer runs.

What this error means

Tests fail with System.TypeInitializationException: The type initializer for X threw an exception, with the actual fault in the InnerException. It reproduces for the CI environment.

dotnet
System.TypeInitializationException: The type initializer for 'AppConfig' threw an exception.
 ---> System.InvalidOperationException: Connection string 'Default' was not found.

Common causes

Static initializer depends on missing CI config

A static field reads a connection string, env var, or config file that exists locally but is not provided in CI, so initialization throws.

A failing static computation

A static constructor does real work (parsing, IO, reflection) that throws under CI conditions.

How to fix it

Read the inner exception and supply the dependency

  1. Open the InnerException - it names the real fault.
  2. Provide the missing env var/connection string in CI, or make the static initializer tolerant of absence.
  3. Re-run the tests.

How to prevent it

  • Avoid heavy work in static constructors; prefer lazy, injectable initialization.
  • Provide all required CI environment variables explicitly.

Related guides

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