microsoft/aspire · error · DistributedApplicationException

The connection string of MongoDB replica set member

Error message

The connection string of MongoDB replica set member '{member.Name}' could not be resolved.

What it means

During replica set initialization, the library resolves each member's ConnectionStringExpression to connect and configure the set. If GetValueAsync returns null for a member, it throws DistributedApplicationException stating the member's connection string could not be resolved.

Solutions

  1. Verify each member is a standard MongoDBServerResource created via AddMongoDB so it has a connection string expression
  2. Ensure any reference parameters backing the connection string are provided (via --parameter or user secrets)
  3. Check the member container actually starts (dashboard logs) - endpoint/connection string resolution depends on it
  4. If using custom resources, override/implement ConnectionStringExpression to always return a usable value
Defensive patterns

Strategy: validation

Validate before calling

var cs = await member.ConnectionStringExpression.GetValueAsync(ct);
if (cs is null) throw new InvalidOperationException($"Member {member.Name} has no connection string");

Try / catch

try { /* start app host */ }
catch (DistributedApplicationException ex) when (ex.Message.Contains("could not be resolved")) { /* inspect member resource config/logs */ }

Prevention

When it happens

Trigger: A MongoDB replica set member's connection string expression yields null at configuration time - usually because the member resource has no connection string generation (missing reference parameters, missing endpoint, or resource misconfiguration).

Common situations: Using a custom/derived MongoDB resource without a ConnectionStringExpression; dependency injection of reference parameters that aren't resolved; a member container that failed to start so its endpoint never materialized.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/7245f4b648f4d656. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs:158

                        .ConfigureAwait(false);

                    await evt.Notifications.PublishUpdateAsync(resource, s => s with
                    {
                        State = KnownResourceStates.Starting,
                    }).ConfigureAwait(false);

                    if (membersList.Find(m => !m.TlsEnabled) is { } memberWithoutTls)
                    {
                        // NOTE: TLS is not optional for a replica set here: the `horizons` mechanism used below to advertise
                        // host-reachable addresses to outside clients keys off the SNI of the incoming connection, which
                        // only exists on TLS connections.
                        throw new DistributedApplicationException($"MongoDB replica set member '{memberWithoutTls.Name}' does not have TLS enabled, which is required for members of a replica set. Ensure an HTTPS/TLS certificate is available for the member, for example by trusting the ASP.NET Core developer certificate.");
                    }

                    var memberConnections = await Task.WhenAll(membersList.Select(async member => new MemberConnection(
                        member,
                        await member.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false)
                            ?? throw new DistributedApplicationException($"The connection string of MongoDB replica set member '{member.Name}' could not be resolved.")
                    ))).ConfigureAwait(false);
                    var initialPrimary = memberConnections[0];

                    var memberHosts = await Task.WhenAll(membersList.Select(async m => new MemberHosts(
                        // NOTE: `Internal` represents the host and port that should be accessible from within the MongoDB server's container.
                        // NOTE: We know that the `TargetPort` always has a value (of 27017).
                        Internal: $"{m.Name}:{m.PrimaryEndpoint.TargetPort!.Value}",
                        // NOTE: `External` represents the host and port that would actually be advertised to outside clients, and should as such be accessible from outside the MongoDB server's container.
                        External: await m.PrimaryEndpoint
                            .Property(EndpointProperty.HostAndPort)
                            .GetValueAsync(ct)
                            .ConfigureAwait(false) ?? throw new DistributedApplicationException($"The endpoint of MongoDB replica set member '{m.Name}' could not be resolved.")
                    ))).ConfigureAwait(false);

                    var configured = false;
                    var lastConfigurationError = null as MongoCommandException;
                    for (var retries = 0; retries < MaxRetriesAttempt; retries++)
                    {

View on GitHub (pinned to 25830f84bd)