{"record":{"id":"a089d4fb61174190","repo":"dotnet/orleans","slug":"connection-string-connectionname-was-not-found","errorCode":null,"errorMessage":"Connection string '{connectionName}' was not found.","messagePattern":"Connection string '(.+?)' was not found\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorageProviderBuilder.cs","lineNumber":45,"sourceCode":"            optionsBuilder.Bind(configurationSection);\n            optionsBuilder.Configure<IServiceProvider>((options, services) =>\n            {\n                var serviceKey = configurationSection[\"ServiceKey\"];\n                if (!string.IsNullOrEmpty(serviceKey))\n                {\n                    options.ConfigureCosmosClient(\n                        provider => new ValueTask<CosmosClient>(provider.GetRequiredKeyedService<CosmosClient>(serviceKey)));\n                    return;\n                }\n\n                var connectionName = configurationSection[\"ConnectionName\"];\n                var connectionString = configurationSection[\"ConnectionString\"];\n                if (!string.IsNullOrEmpty(connectionName) && string.IsNullOrEmpty(connectionString))\n                {\n                    connectionString = services.GetRequiredService<IConfiguration>().GetConnectionString(connectionName);\n                    if (string.IsNullOrEmpty(connectionString))\n                    {\n                        throw new InvalidOperationException($\"Connection string '{connectionName}' was not found.\");\n                    }\n                }\n\n                if (!string.IsNullOrEmpty(connectionString))\n                {\n                    options.ConfigureCosmosClient(connectionString);\n                }\n            });\n        });\n    }\n}\n","sourceCodeStart":27,"sourceCodeEnd":57,"githubUrl":"https://github.com/dotnet/orleans/blob/fca799fa70ecb6ad975224271703ca43221f58de/src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorageProviderBuilder.cs#L27-L57","documentation":"Thrown by the CosmosGrainStorageProviderBuilder when configuration specifies a ConnectionName (to look up via IConfiguration.GetConnectionString) but no matching connection string is registered in the application configuration. The code checks: if ConnectionName is non-empty and ConnectionString is empty, it looks up IConfiguration.GetConnectionString(connectionName); if that also returns null/empty, it throws InvalidOperationException.","triggerScenarios":"In the provider builder's PostConfigure step, the configuration section has a ConnectionName key but the IConfiguration system has no connection string registered under that name. Alternatively, both ConnectionName and ConnectionString are empty/unset but the code path still tries the lookup.","commonSituations":"appsettings.json is missing the ConnectionStrings:AzureCosmosDB section. The ConnectionName value in the Orleans config section doesn't match the key under ConnectionStrings. Environment variable for the connection string not set in the deployment. Typo in the connection name. CI/test environment without the connection string configured.","solutions":["Add a ConnectionStrings section to appsettings.json with the matching key, e.g., \"ConnectionStrings\": { \"MyCosmos\": \"AccountEndpoint=...\" }.","Verify the ConnectionName in the Orleans configuration section matches the key under ConnectionStrings exactly.","Alternatively, set the ConnectionString property directly in the Orleans config section instead of using ConnectionName.","Use environment variables or user secrets to provide the connection string in the deployment environment."],"exampleFix":"// before: appsettings.json missing connection string\n{\n  \"Orleans\": { \"Persistence\": { \"Cosmos\": { \"ConnectionName\": \"AzureCosmos\" } } }\n}\n\n// after: add the matching connection string\n{\n  \"ConnectionStrings\": {\n    \"AzureCosmos\": \"AccountEndpoint=https://...;AccountKey=...\"\n  },\n  \"Orleans\": { \"Persistence\": { \"Cosmos\": { \"ConnectionName\": \"AzureCosmos\" } } }\n}","handlingStrategy":"validation","validationCode":"// At startup, verify the connection string resolves\nvar connectionName = configuration[\"Orleans:Persistence:Cosmos:ConnectionName\"];\nif (!string.IsNullOrEmpty(connectionName))\n{\n    var cs = configuration.GetConnectionString(connectionName);\n    if (string.IsNullOrEmpty(cs))\n        throw new InvalidOperationException($\"Connection string '{connectionName}' not found in IConfiguration.\");\n}","typeGuard":null,"tryCatchPattern":"try { await host.StartAsync(ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"was not found\"))\n{\n    logger.LogCritical(ex, \"Cosmos connection string missing — check ConnectionStrings section in config.\");\n    throw;\n}","preventionTips":["Always verify configuration.GetConnectionString(name) returns non-null before starting the silo.","Keep connection string names consistent between the Orleans config section and ConnectionStrings.","Use user secrets in development and environment variables in production for connection strings.","Add a startup health check that validates all connection strings resolve."],"tags":["cosmos-db","connection-string","configuration","iconfiguration","orleans","startup"],"backgroundTag":null,"analyzedSha":"fca799fa70ecb6ad975224271703ca43221f58de","analyzedAt":"2026-08-13T19:55:57.938Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}