{"record":{"id":"6583a494fa9a5409","repo":"clockworklabs/SpacetimeDB","slug":"building-dbconnection-with-a-null-uri-call-withur","errorCode":null,"errorMessage":"Building DbConnection with a null uri. Call WithUri() first.","messagePattern":"Building DbConnection with a null uri\\. Call WithUri\\(\\) first\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sdks/csharp/src/SpacetimeDBClient.cs","lineNumber":33,"sourceCode":"namespace SpacetimeDB\n{\n    public sealed class DbConnectionBuilder<DbConnection>\n        where DbConnection : IDbConnection, new()\n    {\n        readonly DbConnection conn = new();\n\n        string? uri;\n        string? nameOrAddress;\n        string? token;\n        Compression? compression;\n        bool light;\n        bool? confirmedReads;\n\n        public DbConnection Build()\n        {\n            if (uri == null)\n            {\n                throw new InvalidOperationException(\"Building DbConnection with a null uri. Call WithUri() first.\");\n            }\n            if (nameOrAddress == null)\n            {\n                throw new InvalidOperationException(\"Building DbConnection with a null nameOrAddress. Call WithDatabaseName() first.\");\n            }\n            conn.Connect(token, uri, nameOrAddress, compression ?? Compression.Brotli, light, confirmedReads);\n#if UNITY_5_3_OR_NEWER\n            if (SpacetimeDBNetworkManager._instance != null)\n            {\n                SpacetimeDBNetworkManager._instance.AddConnection(conn);\n            }\n#endif\n            return conn;\n        }\n\n        public DbConnectionBuilder<DbConnection> WithUri(string uri)\n        {\n            this.uri = uri;","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/sdks/csharp/src/SpacetimeDBClient.cs#L15-L51","documentation":"DbConnectionBuilder.Build() validates that the required connection parameters were supplied before calling IDbConnection.Connect. The uri (server endpoint, e.g. https://maincloud.spacetimedb.com or a ws:// URL) must be set via WithUri; otherwise Build throws InvalidOperationException synchronously.","triggerScenarios":"Calling builder.Build() without a prior WithUri(...) call, or with the WithUri call skipped by a conditional (config flag / null env variable branch).","commonSituations":"SPACETIMEDB_URI (or equivalent) missing in the environment so a guarded WithUri never ran; refactoring that moved builder calls; sample code trimmed during prototyping.","solutions":["Call WithUri(\"https://your-host\") on the builder before Build()","Load the uri from configuration early and fail fast with your own clear error if it is null/empty","Keep the whole builder chain in one place so WithUri/WithDatabaseName cannot be skipped piecemeal"],"exampleFix":"// before\nvar conn = builder\n    .WithDatabaseName(\"mydb\")\n    .Build(); // no WithUri -> throws\n\n// after\nvar conn = builder\n    .WithUri(uri ?? throw new ConfigException(\"SPACETIMEDB_URI not set\"))\n    .WithDatabaseName(\"mydb\")\n    .Build();","handlingStrategy":"validation","validationCode":"var uri = config[\"SpacetimeDB:Uri\"];\nif (string.IsNullOrWhiteSpace(uri))\n    throw new ConfigException(\"SpacetimeDB:Uri is required before building a connection\");","typeGuard":null,"tryCatchPattern":"try { var conn = builder.Build(); }\ncatch (InvalidOperationException e) when (e.Message.Contains(\"null uri\"))\n{ /* read the endpoint from fallback config and rebuild */ }","preventionTips":["Fail fast on missing config before touching the builder","Keep WithUri and WithDatabaseName adjacent in one construction method"],"tags":["builder","connection","configuration","csharp"],"backgroundTag":"incomplete-builder-configuration","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}