Override Default Endpoint in .NET Aspire

Tuesday, September 2, 2025

I have been using .NET Aspire for a while now, but this is still not something that's obvious to me. One thing that's great about Aspire is it will launch resources, generate endpoint, and pass it to the application. In my case, I want to launch a Mongo DB container and pass the connection string to my application. However, I want it in the format that's easy for me to maintain.

In .NET Aspire MongoDB database integration article, the supported connection string format doesn't meet my requirements, so my first attempt is to retrieve the data, format them and pass it as environment variable. Something along the line:

var mongodb = ...; // for brevity

var username = mongoDB.Resource.UserNameParameter?.Value;
var password = mongoDB.Resource.PasswordParameter?.Value;
var port = mongoDB.Resource.PrimaryEndpoint.Port.ToString();

builder.AddProject<Projects.AsyloSoft_Market_Api>("market-api")
       .WithEnvironment("Database__ConnectionString", $"mongodb://{username}:{password}@localhost:{port}")
       .WaitFor(mongoDB);

However, that didn't work with an error because some data are generated at runtime.

System.InvalidOperationException: 'The endpoint `tcp` is not allocated for the resource `mongo`.'

My second attempt is to override the generated connection string particularly the host port because, by default, .NET Aspire will randomly use unused port which is great, but it becomes unpredictable. By doing this, the connection string is static, and it doesn't have to be passed to the application every time they are launched.

var mongodb = builder.AddMongoDB("mongo")
                .WithEndpoint(27017, 27017, "tcp")
                .WithLifetime(ContainerLifetime.Persistent);

For somewhat reason, this overload tried to create a new endpoint that's mapped to the same target port, which caused conflict and thus error.

Aspire.Hosting.DistributedApplicationException: 'Endpoint with name 'tcp' already exists. ...

After multiple trials and errors, I managed to override the default generated endpoint using a different overload.

var mongodb = builder.AddMongoDB("mongo")
                .WithEndpoint("tcp", (endpointAnnotation) =>
                {
                    endpointAnnotation.Port = 27017;
                    endpointAnnotation.TargetPort = 27017;
                    endpointAnnotation.Protocol = ProtocolType.Tcp;
                })
                .WithLifetime(ContainerLifetime.Persistent);

It is strange to me that two overloads of a method work differently at its core.

GitHub Repository

(TBA)