microsoft/aspire · error · InvalidOperationException
Cannot build connection string for MongoDB replica set…
Error message
Cannot build connection string for MongoDB replica set resource '{Name}' because it does not have any members. What it means
MongoDBReplicaSetResource builds its connection string as a seed list mongodb://host:port,host:port/?replicaSet=... from its member resources. If the replica set has no members registered, there is nothing to build the seed list from, so BuildConnectionString throws this InvalidOperationException.
Solutions
- Register members on the replica set with WithMember (or pass the intended member count) before the app starts / before anything consumes the connection string.
- Check your AppHost code to ensure AddMongoDBReplicaSet is not called with 0 members or members never added due to conditional logic.
- If you only need a single server, use AddMongoDB directly instead of an empty replica set.
Example fix
// before
var rs = builder.AddMongoDBReplicaSet("mongo", 0);
// after
var rs = builder.AddMongoDBReplicaSet("mongo", 3); Defensive patterns
Strategy: validation
Validate before calling
if (replicaSet.Members.Count() == 0)
throw new InvalidOperationException("Replica set must have at least one member before its connection string is used."); Prevention
- Always call WithMember (or provide a positive member count) right after AddMongoDBReplicaSet.
- Avoid conditional logic that can leave a replica set with zero members.
- Use AddMongoDB directly for single-server scenarios.
When it happens
Trigger: Evaluating the replica set's ConnectionStringExpression (e.g. when a consuming project references it) after creating the replica set with zero members — e.g. AddMongoDBReplicaSet called without adding members via WithMember, or a members count of 0.
Common situations: Creating a replica set resource programmatically without adding members; a code path that removes all members before the connection string is resolved at startup; consumer projects binding to the replica set's connection string before WithMember calls complete.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Connection string is unavailable
- The connection string of MongoDB replica set member
- Cannot remove '"))} from the existing MongoDB replica set '…
- Connection string is unavailable
- ConnectionStringAvailableEvent was published for the
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/7e68ecf005477db2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetResource.cs:84
/// </summary>
internal HashSet<string> StoppedMembers { get; } = new(StringComparers.ResourceName);
/// <summary>
/// Whether the replica set has been configured, meaning it can be reported as running again if its members come back.
/// </summary>
internal bool IsConfigured { get; set; }
/// <summary>
/// Gets the MongoDB server resources that are members of this replica set, in the order they were added.
/// </summary>
public IEnumerable<MongoDBServerResource> Members => Annotations.OfType<MongoReplicaSetMemberAnnotation>().Select(a => a.Member);
private ReferenceExpression BuildConnectionString()
{
var membersList = Members.ToList();
if (membersList is [])
{
throw new InvalidOperationException($"Cannot build connection string for MongoDB replica set resource '{Name}' because it does not have any members.");
}
var builder = new ReferenceExpressionBuilder();
// Build the seed list `mongodb://host1:port1,host2:port2,.../?replicaSet=<name>` — see https://www.mongodb.com/docs/manual/reference/connection-string/#dns-seedlist-connection-format
builder.AppendLiteral("mongodb://");
if (SharedUserNameParameter is not null)
{
builder.Append($"{SharedUserNameParameter:uri}:{SharedPasswordParameter:uri}@");
}
else
{
builder.Append($"{MongoDBServerResource.DefaultUserName:uri}:{SharedPasswordParameter:uri}@");
}
for (var i = 0; i < membersList.Count; i++)
{View on GitHub (pinned to 25830f84bd)