{"record":{"id":"6bef3eb7247ad51e","repo":"dotnet/orleans","slug":"this-instance-has-not-been-initialized-ensure-tha","errorCode":null,"errorMessage":"This instance has not been initialized. Ensure that InitializeMembershipTable is called to initialize this instance before use.","messagePattern":"This instance has not been initialized\\. Ensure that InitializeMembershipTable is called to initialize this instance before use\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Cassandra/Orleans.Clustering.Cassandra/CassandraClusteringTable.cs","lineNumber":38,"sourceCode":"    private readonly IServiceProvider _serviceProvider;\n    private ISession? _session;\n    private OrleansQueries? _queries;\n    private readonly string _identifier;\n\n    public CassandraClusteringTable(\n        IOptions<ClusterOptions> clusterOptions,\n        IOptions<CassandraClusteringOptions> options,\n        IOptions<ClusterMembershipOptions> clusterMembershipOptions,\n        IServiceProvider serviceProvider)\n    {\n        _clusterOptions = clusterOptions.Value;\n        _options = options.Value;\n        _identifier = $\"{_clusterOptions.ServiceId}-{_clusterOptions.ClusterId}\";\n        _serviceProvider = serviceProvider;\n        _ttlSeconds = _options.GetCassandraTtlSeconds(clusterMembershipOptions.Value);\n    }\n\n    private ISession Session => _session ?? throw new InvalidOperationException(NotInitializedMessage);\n\n    private OrleansQueries Queries => _queries ?? throw new InvalidOperationException(NotInitializedMessage);\n\n    async Task IMembershipTable.InitializeMembershipTable(bool tryInitTableVersion)\n    {\n        _session = await _options.CreateSessionAsync(_serviceProvider);\n        if (_session is null)\n        {\n            throw new InvalidOperationException($\"Session created from configuration '{nameof(CassandraClusteringOptions)}' is null.\");\n        }\n\n        _queries = await OrleansQueries.CreateInstance(_session);\n\n        await _queries.EnsureTableExistsAsync(_options.InitializeRetryMaxDelay, _ttlSeconds);\n\n        if (tryInitTableVersion)\n            await _queries.EnsureClusterVersionExistsAsync(_options.InitializeRetryMaxDelay, _identifier);\n    }","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/dotnet/orleans/blob/fca799fa70ecb6ad975224271703ca43221f58de/src/Cassandra/Orleans.Clustering.Cassandra/CassandraClusteringTable.cs#L20-L56","documentation":"InvalidOperationException from the Session property getter on CassandraClusteringTable: the backing _session field is null because IMembershipTable.InitializeMembershipTable has not been called (or has not yet completed). The constant NotInitializedMessage names the exact method to call. This is a usage-ordering error, not a connectivity error.","triggerScenarios":"Any membership operation (ReadAll, ReadRow, InsertRow, etc.) that touches the Session property before InitializeMembershipTable finishes assigning _session. The property is accessed on every query execution.","commonSituations":"Calling membership methods in a test without awaiting initialization, a DI/registration order problem where the table is resolved and used before the hosting pipeline runs initialization, or InitializeMembershipTable throwing earlier (e.g. CreateSessionAsync failed) leaving _session null.","solutions":["Ensure the Orleans hosting extension (UseCassandraClustering / AddCassandraClustering) is registered so the runtime invokes InitializeMembershipTable during silo startup.","Await InitializeMembershipTable before any membership call in tests/integration code.","If initialization is failing earlier, check the log for the exception from CreateSessionAsync (which sets _session); fixing that resolves this secondary error."],"exampleFix":"// before (test)\nvar table = host.Services.GetRequiredService<IMembershipTable>();\nawait table.ReadAll(); // throws: Session null\n\n// after\nvar table = host.Services.GetRequiredService<IMembershipTable>();\nawait table.InitializeMembershipTable(tryInitTableVersion: true);\nawait table.ReadAll();","handlingStrategy":"validation","validationCode":"await table.InitializeMembershipTable(tryInitTableVersion: true);\n// guard before use if you cannot guarantee init order:\nif (!initialized) throw new InvalidOperationException(\"init first\");","typeGuard":null,"tryCatchPattern":"try { await table.ReadAll(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"InitializeMembershipTable\"))\n{\n    logger.LogError(ex, \"Membership table used before initialization\"); throw;\n}","preventionTips":["Register Cassandra clustering through the hosting extension","Await InitializeMembershipTable in tests","Check logs for earlier CreateSessionAsync failures"],"tags":["initialization","cassandra","clustering","usage-ordering"],"backgroundTag":null,"analyzedSha":"fca799fa70ecb6ad975224271703ca43221f58de","analyzedAt":"2026-08-13T19:55:57.938Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}