{"record":{"id":"8eca9ef4eda3a9ad","repo":"microsoft/aspire","slug":"the-connection-string-name-does-not-exist-or-is-missing-the","errorCode":null,"errorMessage":"The connection string '{name}' does not exist or is missing the container name or database name.","messagePattern":"The connection string '(.+?)' does not exist or is missing the container name or database name\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs","lineNumber":139,"sourceCode":"    /// must contain the database name and container name or be set in the <paramref name=\"configureSettings\" />\n    /// callback. To interact with multiple containers against the same database, use\n    /// <see cref=\"CosmosDatabaseBuilder\"/> to register the database and then call\n    /// <see cref=\"CosmosDatabaseBuilder.AddKeyedContainer(string)\"/> for each container.\n    /// </remarks>\n    /// <exception cref=\"InvalidOperationException\">If required ConnectionString is not provided in configuration section</exception>\n    public static void AddKeyedAzureCosmosContainer(\n        this IHostApplicationBuilder builder,\n        string name,\n        Action<MicrosoftAzureCosmosSettings>? configureSettings = null,\n        Action<CosmosClientOptions>? configureClientOptions = null)\n    {\n        var settings = builder.GetSettings(name, configureSettings);\n        var clientOptions = builder.GetClientOptions(settings, configureClientOptions);\n        builder.Services.AddKeyedSingleton(name, (sp, key) =>\n        {\n            if (string.IsNullOrEmpty(settings.ContainerName) || string.IsNullOrEmpty(settings.DatabaseName))\n            {\n                throw new InvalidOperationException($\"The connection string '{name}' does not exist or is missing the container name or database name.\");\n            }\n            var client = GetCosmosClient(name, settings, clientOptions);\n            return client.GetContainer(settings.DatabaseName, settings.ContainerName);\n        });\n    }\n\n    /// <summary>\n    /// Registers the <see cref=\"Database\"/> as a singleton the services provided by the <paramref name=\"builder\"/>\n    /// and returns a <see cref=\"CosmosDatabaseBuilder\"/> to support chaining multiple container registrations against the same database.\n    /// </summary>\n    /// <param name=\"builder\">The <see cref=\"IHostApplicationBuilder\" /> to read config from and add services to.</param>\n    /// <param name=\"connectionName\">The connection name to use to find a connection string.</param>\n    /// <param name=\"configureSettings\">An optional method that can be used for customizing the <see cref=\"MicrosoftAzureCosmosSettings\"/>. It's invoked after the settings are read from the configuration.</param>\n    /// <param name=\"configureClientOptions\">An optional method that can be used for customizing the <see cref=\"CosmosClientOptions\"/>.</param>\n    /// <remarks>Reads the configuration from \"Aspire:Microsoft:Azure:Cosmos:{name}\" section.</remarks>\n    /// <exception cref=\"InvalidOperationException\">If required ConnectionString is not provided in configuration section</exception>\n    public static CosmosDatabaseBuilder AddAzureCosmosDatabase(\n        this IHostApplicationBuilder builder,","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs#L121-L157","documentation":"The keyed variant of AddAzureCosmosContainer behaves identically but registers the Container under the keyed service name '{name}'. It throws the same way when the resolved settings lack DatabaseName or ContainerName.","triggerScenarios":"Calling AddKeyedAzureCosmosContainer(name) where ConnectionStrings has no '{name}' entry or it lacks database/container information, and 'Aspire:Microsoft:Azure:Cosmos:{name}' does not supply DatabaseName/ContainerName.","commonSituations":"Keyed name not matching any configuration; configuring a different key than the one passed as name; forgetting that keyed registrations resolve via IKeyedServiceProvider with the same name used at registration.","solutions":["Configure 'Aspire:Microsoft:Azure:Cosmos:{name}': { \"DatabaseName\": \"...\", \"ContainerName\": \"...\" } with {name} exactly matching the registration name.","Set settings.DatabaseName and settings.ContainerName via the configureSettings callback.","Ensure ConnectionStrings contains an entry for '{name}' with valid Cosmos connection information.","Confirm you resolve the container with the same keyed name (e.g. AddKeyedAzureCosmosClient/container consistency)."],"exampleFix":"// before\nbuilder.AddKeyedAzureCosmosContainer(\"cart\"); // no config for 'cart'\n// after\n// appsettings.json:\n// \"Aspire:Microsoft:Azure:Cosmos:cart\": { \"DatabaseName\": \"shop\", \"ContainerName\": \"cart\" }\nbuilder.AddKeyedAzureCosmosContainer(\"cart\");","handlingStrategy":"validation","validationCode":"var section = builder.Configuration.GetSection($\"Aspire:Microsoft:Azure:Cosmos:{name}\");\nif (string.IsNullOrEmpty(section[\"DatabaseName\"]) || string.IsNullOrEmpty(section[\"ContainerName\"]))\n    throw new InvalidOperationException($\"Keyed Cosmos container '{name}' needs DatabaseName and ContainerName configured.\");","typeGuard":null,"tryCatchPattern":"try { sp.GetRequiredKeyedService<Container>(\"cart\"); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"missing the container name or database name\"))\n{\n    logger.LogError(ex, \"Keyed Cosmos container 'cart' missing db/container config.\");\n    throw;\n}","preventionTips":["Ensure the keyed registration name exactly matches the config section key and resolution key.","Centralize keyed Cosmos names in constants to avoid mismatches.","Test keyed resolution at host startup."],"tags":["azure","cosmos","configuration","keyed-services"],"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"}