microsoft/semantic-kernel · critical · KeyNotFoundException

Could not find configuration section

Error message

Could not find configuration section {caller}

What it means

TestConfiguration.LoadRequiredSection uses IConfiguration.GetSection(caller).Get<T>() to bind a configuration section (named after the calling property via [CallerMemberName]) to a strongly-typed object. If the section is absent or binding yields null, it throws KeyNotFoundException. This enforces that required configuration sections (AzureOpenAIConfig, ApplicationInsightsConfig, etc.) exist at startup.

Solutions

  1. Add the missing section to user secrets: 'dotnet user-secrets set "AzureOpenAIConfig:Endpoint" "<value>"' or the equivalent for your section.
  2. Set the matching environment variables (use __ as the section separator, e.g., AzureOpenAIConfig__ApiKey).
  3. Verify the [CallerMemberName] property name exactly matches the configuration section name you expect.
  4. Ensure TestConfiguration's static constructor / initialization has run and the IConfigurationRoot is built from all expected sources.

Example fix

// before
private static T LoadRequiredSection<T>([CallerMemberName] string? caller = null)
{
    var section = LoadSection<T>(caller);
    if (section is not null) return section;
    throw new KeyNotFoundException($"Could not find configuration section {caller}");
}

// after — list all available sections for faster debugging
private static T LoadRequiredSection<T>([CallerMemberName] string? caller = null)
{
    var section = LoadSection<T>(caller);
    if (section is not null) return section;
    var available = string.Join(", ", s_instance._configRoot.GetChildren().Select(c => c.Key));
    throw new KeyNotFoundException(
        $"Could not find configuration section '{caller}'. Available sections: {available}");
}
Defensive patterns

Strategy: validation

Validate before calling

// Check the section exists before binding
var section = config.GetSection(sectionName);
if (!section.Exists())
    throw new KeyNotFoundException($"Section '{sectionName}' missing. Available: {string.Join(", ", config.GetChildren().Select(c => c.Key))}");

Type guard

bool SectionExists(IConfiguration config, string name) => config.GetSection(name).Exists();

Prevention

When it happens

Trigger: Accessing a TestConfiguration property (e.g., TestConfiguration.AzureOpenAIConfig) when the corresponding configuration section is missing from user secrets, environment variables, or appsettings.json. The section name is derived from the property name via [CallerMemberName].

Common situations: User secrets not set for the required section; environment variable prefix doesn't match the section hierarchy; the section key name has a typo or casing mismatch; running tests in CI without configuring secrets; the IConfigurationRoot wasn't initialized properly (s_instance not built).

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/bb6502734706fe88. Report an issue: GitHub.

Appendix: source

Thrown at dotnet/samples/Demos/TelemetryWithAppInsights/TestConfiguration.cs:59

        }

        if (string.IsNullOrEmpty(caller))
        {
            throw new ArgumentNullException(nameof(caller));
        }

        return s_instance._configRoot.GetSection(caller).Get<T>();
    }

    private static T LoadRequiredSection<T>([CallerMemberName] string? caller = null)
    {
        var section = LoadSection<T>(caller);
        if (section is not null)
        {
            return section;
        }

        throw new KeyNotFoundException($"Could not find configuration section {caller}");
    }

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor.
    public class AzureOpenAIConfig
    {
        public string ChatDeploymentName { get; set; }
        public string ChatModelId { get; set; }
        public string Endpoint { get; set; }
        public string ApiKey { get; set; }
    }

    public class ApplicationInsightsConfig
    {
        public string ConnectionString { get; set; }
    }

    public class GoogleAIConfig
    {

View on GitHub (pinned to c028a0c7dc)