A .NET service can work perfectly when started directly from Visual Studio or the command line but fail when the same service runs inside a .NET Aspire application. The API may return connection errors, configuration values may be missing, database connections may fail, or requests to other services may time out.
These problems can be confusing because the application code has not changed. The difference is usually the environment in which the service runs.
.NET Aspire manages application resources, environment variables, endpoints, service discovery, startup dependencies, and container networking. When a service runs independently, it may rely on local configuration, fixed ports, or services already running on the developer's machine. Inside Aspire, those assumptions may no longer be valid.
For example, an API that connects to Redis through localhost:6379 may work when Redis is running directly on the developer's machine. However, the same configuration can fail when Redis runs inside an Aspire-managed container.
In this article, we will explore why services behave differently inside Aspire and how to identify and fix common configuration, networking, dependency, and startup problems.
Understanding Why a Service Behaves Differently in Aspire
When an ASP.NET Core application runs independently, its configuration usually comes from sources such as:
- appsettings.json.
- appsettings.Development.json.
- User Secrets.
- Environment variables.
- Command-line arguments.
- Custom configuration providers.
When the same application runs through Aspire, the AppHost can introduce additional configuration through resource references and environment variables.
Aspire can also manage endpoints and service discovery differently from a manually started application.
Consider a simple architecture:
Commerce Application
│
├── Commerce.AppHost
│
├── Commerce.Api
│
├── Commerce.Worker
│
├── PostgreSQL Container
│
└── Redis ContainerWhen Commerce.Api runs independently, it might use a local PostgreSQL connection string and a fixed Redis endpoint.
When Commerce.AppHost starts the application, Aspire can provide connection information for PostgreSQL and Redis through resource references.
If the application still expects the old local configuration, it may attempt to connect to the wrong endpoint.
The first step in troubleshooting is understanding what changed between the standalone environment and the Aspire-managed environment.
Checking Environment Variables and Configuration Values
One of the most common reasons a service fails inside Aspire is that it receives different configuration values.
For example, consider this configuration:
{
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Port=5432;Database=CommerceDb;Username=postgres;Password=local"
}
}This configuration may work when PostgreSQL runs directly on the developer's machine.
However, if PostgreSQL is running inside an Aspire-managed container, the application should use the connection information provided by Aspire rather than assuming that localhost:5432 points to the database container.
The AppHost should define the database resource and reference it from the API:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres")
.WithDataVolume();
var database = postgres.AddDatabase("commerce");
builder.AddProject<Projects.Commerce_Api>("commerce-api")
.WithReference(database)
.WaitFor(database);
builder.Build().Run();Inside the API project, consume the database using the appropriate Aspire integration:
var builder = WebApplication.CreateBuilder(args);
builder.AddNpgsqlDbContext<CommerceDbContext>("commerce");
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();This requires the corresponding PostgreSQL Entity Framework Core Aspire integration package in the API project.
The important detail is that the database resource name used by the application must match the resource name registered in the AppHost.
If the API still reads an old hardcoded connection string, the Aspire-provided configuration may not be used as expected.
To investigate configuration problems, inspect the service environment variables through the Aspire dashboard or temporarily log non-sensitive configuration values.
Never log passwords, access tokens, or complete connection strings containing credentials.
Understanding Localhost and Container Networking
Networking is another common source of failures.
The meaning of localhost depends on where the application is running.
When an API runs directly on the developer's machine, localhost refers to the developer's machine.
When an API runs inside a container, localhost refers to that container itself.
Consider this architecture:
Developer Machine
│
├── Commerce API
│
└── Aspire Container Network
│
├── PostgreSQL
└── RedisIf the API runs as a host process, it needs to connect through an endpoint accessible from the host. If the API runs inside a container, it can communicate with other containers through the container network.
Aspire manages container networking and provides resource names for communication between container resources in the same AppHost.
For example, a containerized service can reach a Redis resource named redis through the container network using its resource name and target port.
However, a host-based .NET project does not automatically use the same container DNS environment. It relies on the endpoint information provided by Aspire.
This is why hardcoding localhost, container names, or fixed ports can cause problems when moving between execution environments.
The recommended approach is to use resource references and service discovery rather than manually constructing service URLs.
Fixing Service-to-Service Communication Problems
Suppose Commerce.Api needs to call Inventory.Api.
When both projects are managed by the same AppHost, define their relationship using WithReference.
var builder = DistributedApplication.CreateBuilder(args);
var inventoryApi = builder.AddProject<Projects.Inventory_Api>(
"inventory-api");
builder.AddProject<Projects.Commerce_Api>("commerce-api")
.WithReference(inventoryApi);
builder.Build().Run();The reference provides the endpoint information needed by the consuming application.
In the Commerce API, register an HttpClient with service discovery:
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.Services.AddHttpClient<InventoryClient>(
client =>
{
client.BaseAddress = new Uri("https://inventory-api");
});
var app = builder.Build();
app.MapDefaultEndpoints();
app.Run();The AddServiceDefaults method is part of the shared ServiceDefaults project in a typical Aspire solution. It configures common functionality, including service discovery.
The service name in the URI must match the resource name registered in the AppHost.
Also, the endpoint scheme must match the endpoint exposed by the referenced service. Aspire supports service discovery for configured HTTP endpoints, including named endpoints.
If service discovery fails, verify that:
- The consumer has a reference to the producer.
- The service name matches exactly.
- The producer exposes the expected endpoint.
- Service discovery is registered in the consuming application.
- The configured HTTP or HTTPS scheme matches the available endpoint.
Avoid replacing service discovery with fixed localhost URLs simply to make the application work locally. That can hide the actual configuration problem and create failures in other environments.
Checking Startup Dependencies and Readiness
A service can also fail because it starts before its dependency is ready.
For example, an API may attempt to connect to PostgreSQL immediately after startup. If PostgreSQL is still initializing, the first connection attempt can fail.
A common AppHost configuration is:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres")
.WithDataVolume();
var database = postgres.AddDatabase("commerce");
builder.AddProject<Projects.Commerce_Api>("commerce-api")
.WithReference(database)
.WaitFor(database);
builder.Build().Run();The WithReference method establishes the dependency and provides resource information. WaitFor adds a startup readiness dependency.
For resources that support health checks, readiness can be based on the resource's health status rather than simply whether its process has started.
Join the conversation! Your thoughts help the community grow.