{"record":{"id":"bb6502734706fe88","repo":"microsoft/semantic-kernel","slug":"could-not-find-configuration-section-caller","errorCode":null,"errorMessage":"Could not find configuration section {caller}","messagePattern":"Could not find configuration section (.+?)","errorType":"exception","errorClass":"KeyNotFoundException","httpStatus":null,"severity":"critical","filePath":"dotnet/samples/Demos/TelemetryWithAppInsights/TestConfiguration.cs","lineNumber":59,"sourceCode":"        }\n\n        if (string.IsNullOrEmpty(caller))\n        {\n            throw new ArgumentNullException(nameof(caller));\n        }\n\n        return s_instance._configRoot.GetSection(caller).Get<T>();\n    }\n\n    private static T LoadRequiredSection<T>([CallerMemberName] string? caller = null)\n    {\n        var section = LoadSection<T>(caller);\n        if (section is not null)\n        {\n            return section;\n        }\n\n        throw new KeyNotFoundException($\"Could not find configuration section {caller}\");\n    }\n\n#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor.\n    public class AzureOpenAIConfig\n    {\n        public string ChatDeploymentName { get; set; }\n        public string ChatModelId { get; set; }\n        public string Endpoint { get; set; }\n        public string ApiKey { get; set; }\n    }\n\n    public class ApplicationInsightsConfig\n    {\n        public string ConnectionString { get; set; }\n    }\n\n    public class GoogleAIConfig\n    {","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/TelemetryWithAppInsights/TestConfiguration.cs#L41-L77","documentation":"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.","triggerScenarios":"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].","commonSituations":"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).","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."],"exampleFix":"// before\nprivate static T LoadRequiredSection<T>([CallerMemberName] string? caller = null)\n{\n    var section = LoadSection<T>(caller);\n    if (section is not null) return section;\n    throw new KeyNotFoundException($\"Could not find configuration section {caller}\");\n}\n\n// after — list all available sections for faster debugging\nprivate static T LoadRequiredSection<T>([CallerMemberName] string? caller = null)\n{\n    var section = LoadSection<T>(caller);\n    if (section is not null) return section;\n    var available = string.Join(\", \", s_instance._configRoot.GetChildren().Select(c => c.Key));\n    throw new KeyNotFoundException(\n        $\"Could not find configuration section '{caller}'. Available sections: {available}\");\n}","handlingStrategy":"validation","validationCode":"// Check the section exists before binding\nvar section = config.GetSection(sectionName);\nif (!section.Exists())\n    throw new KeyNotFoundException($\"Section '{sectionName}' missing. Available: {string.Join(\", \", config.GetChildren().Select(c => c.Key))}\");","typeGuard":"bool SectionExists(IConfiguration config, string name) => config.GetSection(name).Exists();","tryCatchPattern":null,"preventionTips":["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."],"tags":["configuration","key-not-found","secrets","test-configuration","startup"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}