{"record":{"id":"ece163f0a9fb70ed","repo":"microsoft/aspire","slug":"missing-required-configuration-for-key-valid-values-are","errorCode":null,"errorMessage":"Missing required configuration for {key}. Valid values are {string.Join(\", \", Enum.GetNames(typeof(T)))}.","messagePattern":"Missing required configuration for (.+?)\\. Valid values are (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/IConfigurationExtensions.cs","lineNumber":222,"sourceCode":"\n    /// <summary>\n    /// Gets the specified required configuration value as a member of an enum.\n    /// </summary>\n    /// <remarks>\n    /// Parsing is case-insensitive.\n    /// </remarks>\n    /// <param name=\"configuration\">The <see cref=\"IConfiguration\"/> this method extends.</param>\n    /// <param name=\"key\">The configuration key.</param>\n    /// <exception cref=\"InvalidOperationException\">The configuration value is empty or not a valid member of the enum.</exception>\n    /// <returns>The parsed enum member.</returns>\n    public static T GetEnum<T>(this IConfiguration configuration, string key)\n        where T : struct\n    {\n        var value = configuration.GetEnum<T>(key, defaultValue: null);\n\n        if (value is null)\n        {\n            throw new InvalidOperationException($\"Missing required configuration for {key}. Valid values are {string.Join(\", \", Enum.GetNames(typeof(T)))}.\");\n        }\n\n        return value.Value;\n    }\n\n    /// <summary>\n    /// Gets a configuration value with support for dash-to-underscore normalization.\n    /// First tries the exact configuration key, then tries with dashes replaced by underscores.\n    /// </summary>\n    /// <remarks>\n    /// This supports command-line arguments and environment variables where dashes are replaced with underscores.\n    /// For example, a parameter named \"my-param\" can be resolved from configuration key \"my_param\".\n    /// </remarks>\n    /// <param name=\"configuration\">The <see cref=\"IConfiguration\"/> this method extends.</param>\n    /// <param name=\"configKey\">The configuration key to look up.</param>\n    /// <returns>The configuration value, or <see langword=\"null\"/> if not found.</returns>\n    public static string? GetValueWithNormalizedKey(this IConfiguration configuration, string configKey)\n    {","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Shared/IConfigurationExtensions.cs#L204-L240","documentation":"IConfiguration.GetEnum<T>(key) is the required-value overload: it reads the configuration key and throws InvalidOperationException when the key is absent or empty, because a required enum setting could not be resolved. The message lists every valid enum member name so the developer knows what to set. Parsing itself (in the optional overload) is case-insensitive via Enum.TryParse.","triggerScenarios":"Calling configuration.GetEnum<T>(\"SomeKey\") (no defaultValue) where the key is missing from appsettings.json/environment variables/user secrets, or where the value is an empty string.","commonSituations":"Deploying to an environment where appsettings.json or the expected environment variable was not copied; renaming a config key in code without updating configuration files; forgetting user secrets locally; CI/container images missing the settings file.","solutions":["Add the missing key to your configuration (appsettings.json, environment variable, or user secrets) with a valid enum member name, e.g. \"SomeKey\": \"ValidValue\".","Verify the key spelling matches the key passed to GetEnum<T>.","If the value should be optional, call the overload with a default: configuration.GetEnum<T>(key, defaultValue: SomeEnum.Default).","Confirm the configuration source (JSON file, env vars) is actually loaded into the IConfiguration built at startup."],"exampleFix":"// before (appsettings.json missing the key)\n{ }\nvar mode = config.GetEnum<CacheMode>(\"Cache:Mode\");\n\n// after\n{ \"Cache\": { \"Mode\": \"Distributed\" } }\nvar mode = config.GetEnum<CacheMode>(\"Cache:Mode\");","handlingStrategy":"validation","validationCode":"var raw = config[\"Cache:Mode\"];\nif (string.IsNullOrWhiteSpace(raw))\n    throw new InvalidOperationException(\"Cache:Mode is required. Set it to one of: \" + string.Join(\", \", Enum.GetNames<CacheMode>()));","typeGuard":null,"tryCatchPattern":"try { var mode = config.GetEnum<CacheMode>(\"Cache:Mode\"); }\ncatch (InvalidOperationException ex) { logger.LogError(ex, \"Missing/invalid enum config\"); throw; }","preventionTips":["Validate all required config keys at startup before serving traffic.","Use a strongly typed options class bound at startup so missing keys fail fast in one place.","Keep appsettings.json, user secrets, and env vars in sync across environments."],"tags":["configuration","enum","missing-value"],"backgroundTag":"missing-required-config-field","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}