{"record":{"id":"4e8035d106f85456","repo":"microsoft/aspire","slug":"failed-to-configure-mongodb-replica-set-resource-resource","errorCode":null,"errorMessage":"Failed to configure MongoDB replica set resource '{resource.Name}' after {MaxRetriesAttempt} attempts.{(lastConfigurationError is null ? \"\" : $\" The last error reported by MongoDB was: {lastConfigurationError.Message}\")}","messagePattern":"Failed to configure MongoDB replica set resource '(.+?)' after (.+?) attempts\\.(.+?)\"\\)\\}","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs","lineNumber":361,"sourceCode":"                                logger.LogInformation(\"MongoDB replica set member '{MemberName}' stopped accepting connections before initialization completed\", initialPrimary.Resource.Name);\n                            }\n                        }\n\n                        if (!configured && retries < MaxRetriesAttempt - 1)\n                        {\n                            logger.LogInformation(\"MongoDB replica set configuration retry attempt {Current}/{Max} will begin after {WaitIntervalSeconds} seconds\", retries + 1, MaxRetriesAttempt, s_rsInitiationRetryWaitInterval.TotalSeconds);\n                            await Task.Delay(s_rsInitiationRetryWaitInterval, ct).ConfigureAwait(false);\n                        }\n                    }\n\n                    if (!configured)\n                    {\n                        // NOTE: Every attempt ran into a retryable error. The replica set is at best partially configured at\n                        // this point, so it must not be reported as running.\n                        // NOTE: The last error MongoDB gave is carried into the message, because a configuration it will\n                        // never accept fails exactly like one that simply lost a race, and only the server's own\n                        // explanation tells the two apart.\n                        throw new DistributedApplicationException(\n                            $\"Failed to configure MongoDB replica set resource '{resource.Name}' after {MaxRetriesAttempt} attempts.{(lastConfigurationError is null ? \"\" : $\" The last error reported by MongoDB was: {lastConfigurationError.Message}\")}\",\n                            lastConfigurationError!);\n                    }\n\n                    rsResource.IsConfigured = true;\n                    await evt.Notifications.PublishUpdateAsync(resource, s => s with\n                    {\n                        State = KnownResourceStates.Running,\n                    }).ConfigureAwait(false);\n                }\n                catch (Exception ex)\n                {\n                    logger.LogCritical(ex, \"Failed to initialize MongoDB replica set resource '{ResourceName}'\", resource.Name);\n                    await evt.Notifications.PublishUpdateAsync(resource, s => s with\n                    {\n                        State = KnownResourceStates.FailedToStart,\n                    }).ConfigureAwait(false);\n                }","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs#L343-L379","documentation":"After exhausting MaxRetriesAttempt attempts to configure the MongoDB replica set (e.g. running `replSetInitiate`/`reconfig`), the library throws DistributedApplicationException stating configuration failed, optionally including the last error MongoDB itself reported. The replica set is at best partially configured at this point.","triggerScenarios":"Every retry of replica set configuration hit a retryable error - MongoDB rejecting the configuration command, members unreachable, elections failing, or a configuration MongoDB will never accept (which fails identically to a transient race).","commonSituations":"Members not yet listening when config started; MongoDB rejecting member host lists (duplicate/unreachable hosts); network/DNS issues inside the container network; the server's last error message revealing the actual cause.","solutions":["Read the inner exception / 'last error reported by MongoDB' portion of the message for the actual server-side cause","Verify all member containers started and are reachable on their internal host:port before the replica set initializes","Check for configuration MongoDB permanently rejects (duplicate hosts, wrong member counts) - only the server's error distinguishes this from a transient race","Restart the app host; the library retries on the next run and often succeeds after transient races"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { /* run app host */ }\ncatch (DistributedApplicationException ex) when (ex.Message.Contains(\"Failed to configure MongoDB replica set\")) { /* read inner exception for the server's last error; retry the run */ }","preventionTips":["Ensure members are healthy and reachable before initialization","Check the inner/last MongoDB error to distinguish transient races from permanent config rejections","Keep member host lists valid (no duplicates, correct count)"],"tags":["mongodb","replica-set","retry-exhausted","configuration"],"backgroundTag":"request-timeout","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}