microsoft/semantic-kernel · critical · KeyNotFoundException
Could not find configuration section {caller}
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.
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)
Solutions
- Add the missing section to user secrets: 'dotnet user-secrets set "AzureOpenAIConfig:Endpoint" "<value>"' or the equivalent for your section.
- Set the matching environment variables (use __ as the section separator, e.g., AzureOpenAIConfig__ApiKey).
- Verify the [CallerMemberName] property name exactly matches the configuration section name you expect.
- 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
- Document every required configuration section in a README or appsettings template.
- Use the double-underscore convention for environment variables (Section__Key).
- Log all available top-level sections at startup to diagnose missing ones quickly.
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
- OpenAI/Azure OpenAI configuration was not found.
- Configuration is not provided.
- AI Chat Service type '{appConfig.RagConfig.AIChatService}' i
- AI Embedding Service type '{appConfig.RagConfig.AIEmbeddingS
- Vector store type '{appConfig.RagConfig.VectorStoreType}' is
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/bb6502734706fe88.
Report an issue: GitHub.