microsoft/aspire · error · DistributedApplicationException
Failed to configure MongoDB replica set resource
Error message
Failed to configure MongoDB replica set resource '{resource.Name}' after {MaxRetriesAttempt} attempts.{(lastConfigurationError is null ? "" : $" The last error reported by MongoDB was: {lastConfigurationError.Message}")} What it means
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.
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
Defensive patterns
Strategy: retry
Try / catch
try { /* run app host */ }
catch (DistributedApplicationException ex) when (ex.Message.Contains("Failed to configure MongoDB replica set")) { /* read inner exception for the server's last error; retry the run */ } Prevention
- 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)
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: Request timed out: what client-side request timeouts mean across libraries (Request timed out, TIMED_OUT, APITimeoutError) — this error's family across 39 libraries.
Related errors
- A database name is required but was not provided. Specify…
- Cannot build connection string for MongoDB replica set…
- Cannot remove '"))} from the existing MongoDB replica set '…
- Connection string is unavailable
- MongoDB replica set member
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/4e8035d106f85456.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs:361
logger.LogInformation("MongoDB replica set member '{MemberName}' stopped accepting connections before initialization completed", initialPrimary.Resource.Name);
}
}
if (!configured && retries < MaxRetriesAttempt - 1)
{
logger.LogInformation("MongoDB replica set configuration retry attempt {Current}/{Max} will begin after {WaitIntervalSeconds} seconds", retries + 1, MaxRetriesAttempt, s_rsInitiationRetryWaitInterval.TotalSeconds);
await Task.Delay(s_rsInitiationRetryWaitInterval, ct).ConfigureAwait(false);
}
}
if (!configured)
{
// NOTE: Every attempt ran into a retryable error. The replica set is at best partially configured at
// this point, so it must not be reported as running.
// NOTE: The last error MongoDB gave is carried into the message, because a configuration it will
// never accept fails exactly like one that simply lost a race, and only the server's own
// explanation tells the two apart.
throw new DistributedApplicationException(
$"Failed to configure MongoDB replica set resource '{resource.Name}' after {MaxRetriesAttempt} attempts.{(lastConfigurationError is null ? "" : $" The last error reported by MongoDB was: {lastConfigurationError.Message}")}",
lastConfigurationError!);
}
rsResource.IsConfigured = true;
await evt.Notifications.PublishUpdateAsync(resource, s => s with
{
State = KnownResourceStates.Running,
}).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogCritical(ex, "Failed to initialize MongoDB replica set resource '{ResourceName}'", resource.Name);
await evt.Notifications.PublishUpdateAsync(resource, s => s with
{
State = KnownResourceStates.FailedToStart,
}).ConfigureAwait(false);
}View on GitHub (pinned to 25830f84bd)