{"record":{"id":"5c0fd0b796b6423d","repo":"microsoft/aspire","slug":"the-connection-string-connectionname-does-not-exist-or-is-5c0fd0","errorCode":null,"errorMessage":"The connection string '{connectionName}' 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":74,"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 AddAzureCosmosContainer(\n        this IHostApplicationBuilder builder,\n        string connectionName,\n        Action<MicrosoftAzureCosmosSettings>? configureSettings = null,\n        Action<CosmosClientOptions>? configureClientOptions = null)\n    {\n        var settings = builder.GetSettings(connectionName, configureSettings);\n        var clientOptions = builder.GetClientOptions(settings, configureClientOptions);\n        builder.Services.AddSingleton(sp =>\n        {\n            if (string.IsNullOrEmpty(settings.ContainerName) || string.IsNullOrEmpty(settings.DatabaseName))\n            {\n                throw new InvalidOperationException($\"The connection string '{connectionName}' does not exist or is missing the container name or database name.\");\n            }\n            var client = GetCosmosClient(connectionName, settings, clientOptions);\n            return client.GetContainer(settings.DatabaseName, settings.ContainerName);\n        });\n    }\n\n    /// <summary>\n    /// Registers the <see cref=\"CosmosClient\" /> as a singleton for given <paramref name=\"name\" /> in the services provided by the <paramref name=\"builder\"/>.\n    /// Configures logging and telemetry for the <see cref=\"CosmosClient\" />.\n    /// </summary>\n    /// <param name=\"builder\">The <see cref=\"IHostApplicationBuilder\" /> to read config from and add services to.</param>\n    /// <param name=\"name\">The name of the component, which is used as the <see cref=\"ServiceDescriptor.ServiceKey\"/> of the service and also to retrieve the connection string from the ConnectionStrings configuration section.</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 void AddKeyedAzureCosmosClient(\n        this IHostApplicationBuilder builder,","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs#L56-L92","documentation":"AddAzureCosmosContainer registers a singleton Container. It requires both DatabaseName and ContainerName in the resolved Cosmos settings; if either is missing, no container can be identified and Aspire throws inside the singleton factory.","triggerScenarios":"Calling AddAzureCosmosContainer where the connection string '{connectionName}' does not exist under ConnectionStrings, or exists but does not encode database/container names, and settings.DatabaseName/ContainerName were not provided in the 'Aspire:Microsoft:Azure:Cosmos' config section.","commonSituations":"Connection string is only an account endpoint (no db/container info); DatabaseName/ContainerName keys misspelled or nested at wrong level; calling AddAzureCosmosContainer before AddAzureCosmosClient-style configuration that would populate settings; using account-level connection string intended only for the client API.","solutions":["Set DatabaseName and ContainerName in the 'Aspire:Microsoft:Azure:Cosmos:{connectionName}' config section (or 'Aspire:Microsoft:Azure:Cosmos' globally).","Provide them via the configureSettings callback: settings.DatabaseName = \"db\"; settings.ContainerName = \"items\";.","Ensure ConnectionStrings:{connectionName} exists and, if it carries the database/container, that it is formatted as expected by the component.","Verify the connectionName argument matches the configuration key."],"exampleFix":"// before\nbuilder.AddAzureCosmosContainer(\"cosmos\"); // DatabaseName/ContainerName unset\n// after\n// appsettings.json:\n// \"Aspire:Microsoft:Azure:Cosmos:cosmos\": { \"DatabaseName\": \"todos\", \"ContainerName\": \"items\" }\nbuilder.AddAzureCosmosContainer(\"cosmos\");","handlingStrategy":"validation","validationCode":"var section = builder.Configuration.GetSection(\"Aspire:Microsoft:Azure:Cosmos:cosmos\");\nif (string.IsNullOrEmpty(section[\"DatabaseName\"]) || string.IsNullOrEmpty(section[\"ContainerName\"]))\n    throw new InvalidOperationException(\"Set DatabaseName and ContainerName before calling AddAzureCosmosContainer.\");","typeGuard":null,"tryCatchPattern":"try { container.GetItemQueryIterator<dynamic>(\"SELECT * c\").ReadNextAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"missing the container name or database name\"))\n{\n    logger.LogError(ex, \"Cosmos container/database name missing for 'cosmos'.\");\n    throw;\n}","preventionTips":["Configure DatabaseName/ContainerName per connection under Aspire:Microsoft:Azure:Cosmos:{connectionName}.","Use configureSettings to set names explicitly when they are environment-dependent.","Distinguish account-level client registration from container-level registration and configure each accordingly."],"tags":["azure","cosmos","configuration","connection-string"],"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"}