clockworklabs/SpacetimeDB · error · InvalidOperationException

Building DbConnection with a null uri. Call WithUri() first.

Error message

Building DbConnection with a null uri. Call WithUri() first.

What it means

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.

Source

Thrown at sdks/csharp/src/SpacetimeDBClient.cs:33

namespace SpacetimeDB
{
    public sealed class DbConnectionBuilder<DbConnection>
        where DbConnection : IDbConnection, new()
    {
        readonly DbConnection conn = new();

        string? uri;
        string? nameOrAddress;
        string? token;
        Compression? compression;
        bool light;
        bool? confirmedReads;

        public DbConnection Build()
        {
            if (uri == null)
            {
                throw new InvalidOperationException("Building DbConnection with a null uri. Call WithUri() first.");
            }
            if (nameOrAddress == null)
            {
                throw new InvalidOperationException("Building DbConnection with a null nameOrAddress. Call WithDatabaseName() first.");
            }
            conn.Connect(token, uri, nameOrAddress, compression ?? Compression.Brotli, light, confirmedReads);
#if UNITY_5_3_OR_NEWER
            if (SpacetimeDBNetworkManager._instance != null)
            {
                SpacetimeDBNetworkManager._instance.AddConnection(conn);
            }
#endif
            return conn;
        }

        public DbConnectionBuilder<DbConnection> WithUri(string uri)
        {
            this.uri = uri;

View on GitHub (pinned to 524b4487d9)

Solutions

  1. Call WithUri("https://your-host") on the builder before Build()
  2. Load the uri from configuration early and fail fast with your own clear error if it is null/empty
  3. Keep the whole builder chain in one place so WithUri/WithDatabaseName cannot be skipped piecemeal

Example fix

// before
var conn = builder
    .WithDatabaseName("mydb")
    .Build(); // no WithUri -> throws

// after
var conn = builder
    .WithUri(uri ?? throw new ConfigException("SPACETIMEDB_URI not set"))
    .WithDatabaseName("mydb")
    .Build();
Defensive patterns

Strategy: validation

Validate before calling

var uri = config["SpacetimeDB:Uri"];
if (string.IsNullOrWhiteSpace(uri))
    throw new ConfigException("SpacetimeDB:Uri is required before building a connection");

Try / catch

try { var conn = builder.Build(); }
catch (InvalidOperationException e) when (e.Message.Contains("null uri"))
{ /* read the endpoint from fallback config and rebuild */ }

Prevention

When it happens

Trigger: Calling builder.Build() without a prior WithUri(...) call, or with the WithUri call skipped by a conditional (config flag / null env variable branch).

Common situations: SPACETIMEDB_URI (or equivalent) missing in the environment so a guarded WithUri never ran; refactoring that moved builder calls; sample code trimmed during prototyping.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@524b4487d9 (2026-08-16). Data as JSON: /api/errors/6583a494fa9a5409. Report an issue: GitHub.