Introduction
In the previous lesson, we created the application model for our distributed system with an Aspire AppHost. That model gives us a single place to describe services and infrastructure components. However, listing resources is only the beginning. Each service must also declare which database, cache, message broker, or other service it depends on.
This lesson focuses on three important Aspire resource types: project resources, container resources, and executable resources. We will then connect those resources with WithReference, WaitFor, and WithEnvironment. The goal is to avoid hard-coded ports, machine-specific URLs, and manually maintained connection strings.
1. The Aspire Resource Model
In Aspire, every runnable part of the distributed application is represented as a resource. A resource can be an ASP.NET Core project, a Docker container, a local process, a database, a cache, or a message broker. The AppHost describes how these resources are started, which endpoints they expose, and which other resources they depend on.
Project resources
Use AddProject to register an ASP.NET Core API, worker, or another .NET project from the solution. A project resource points to a real .NET project. Aspire can build and launch it, expose its endpoints in the dashboard, and inject references from other resources through environment variables and configuration.
var catalog = builder
.AddProject<Projects.CatalogService>("catalog")
.WithReference(catalogDb)
.WithReference(cache)
.WaitFor(catalogDb);
The name catalog is the logical name of the resource. It appears in the dashboard and is also used by service discovery. The physical port does not need to remain constant between runs.
Container resources
A container resource runs from a Docker image and is commonly used for infrastructure or packaged applications. Redis, RabbitMQ, SQL Server, or a custom service can be modeled as a container. The AppHost can define its image, tag, endpoints, volumes, and environment variables.
var rabbit = builder
.AddContainer("messaging", "rabbitmq", "3-management")
.WithEndpoint("amqp", endpoint =>
{
endpoint.Port = 5672;
endpoint.TargetPort = 5672;
});
A fixed host port is not always necessary. If you omit it, Aspire can allocate an available host port. Dynamic allocation makes it easier to run multiple AppHost instances without port conflicts.
Executable resources
An executable resource runs directly on the development machine rather than inside a container. This is useful for a Node.js frontend, Python worker, migration utility, or local helper process.
var frontend = builder
.AddExecutable(
"storefront",
"npm",
"../Storefront",
"run",
"dev")
.WithEnvironment("NODE_ENV", "development");
The AddExecutable call receives a resource name, the executable to run, a working directory, and command-line arguments. Because the process runs on the host, the required tool such as npm, node, or a language runtime must be installed locally.
2. References and Dependencies
When one resource needs another, distinguish between a configuration reference and a startup dependency.
WithReference: Passes connection details or service endpoint information to the dependent resource.WaitFor: Controls startup ordering and waits for another resource to become ready.
For example, the Catalog API may need both a database connection and a readiness dependency:
var catalog = builder
.AddProject<Projects.CatalogService>("catalog")
.WithReference(catalogDb)
.WaitFor(catalogDb);
Without WithReference, the application does not automatically receive the database connection information. Using only WaitFor controls the order of startup but does not provide a connection string or service endpoint.
3. Extending the Application Model
Let us connect the Catalog, Order, and Inventory services to SQL Server, Redis, and RabbitMQ. The SQL Server integration creates a database resource, allowing the application to receive a connection string associated with a database name rather than relying on a manually written server address.
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder
.AddSqlServer("sql")
.AddDatabase("catalogdb");
var cache = builder
.AddRedis("cache");
var rabbit = builder
.AddRabbitMQ("messaging");
var catalog = builder
.AddProject<Projects.CatalogService>("catalog")
.WithReference(sql)
.WithReference(cache)
.WaitFor(sql);
var inventory = builder
.AddProject<Projects.InventoryService>("inventory")
.WithReference(sql)
.WithReference(rabbit)
.WaitFor(sql)
.WaitFor(rabbit);
var order = builder
.AddProject<Projects.OrderService>("order")
.WithReference(catalog)
.WithReference(inventory)
.WithReference(sql)
.WithReference(rabbit)
.WaitFor(catalog)
.WaitFor(inventory);
builder.Build().Run();
Although Catalog and Inventory appear to use the same SQL Server resource, the database boundary still matters. In a real microservice system, use a separate database or schema per service where appropriate. Do not let a convenient infrastructure reference silently become shared data ownership.
4. Connection String Propagation
When a resource is connected with WithReference, Aspire passes the required connection information to the dependent application. In .NET configuration, double underscores represent nested keys. A connection string commonly appears as an environment variable such as:
ConnectionStrings__catalogdb
ASP.NET Core can read it with GetConnectionString:
var connectionString = builder.Configuration
.GetConnectionString("catalogdb")
?? throw new InvalidOperationException(
"catalogdb connection string is missing.");
Entity Framework Core can then use the value:
builder.Services.AddDbContext<CatalogDbContext>(options =>
options.UseSqlServer(connectionString));
This removes the need to hard-code a connection string in appsettings.json. Aspire can provide a local container connection during development, while a test or production environment can supply a completely different value without changing application code.
Redis and RabbitMQ configuration
The same pattern applies to Redis and RabbitMQ. The resource name becomes part of the configuration key:
var redisConnection = builder.Configuration
.GetConnectionString("cache");
var rabbitConnection = builder.Configuration
.GetConnectionString("messaging");
Always verify the format expected by the client library. An Aspire connection string and a URL-style environment variable are not necessarily identical. If a third-party application specifically expects REDIS_URL or AMQP_URL, use WithEnvironment to provide that application-specific variable.
5. Service References and Service Discovery
If the Order service calls the Catalog and Inventory APIs over HTTP, it should not depend on hard-coded addresses such as http://localhost:5001. Declare project references in the AppHost and use logical service names from the client application.
var order = builder
.AddProject<Projects.OrderService>("order")
.WithReference(catalog)
.WithReference(inventory);
In the Order project, a service-defaults project can configure service discovery support for HttpClient:
builder.AddServiceDefaults();
builder.Services.AddHttpClient("catalog", client =>
{
client.BaseAddress = new Uri("https+http://catalog");
});
builder.Services.AddHttpClient("inventory", client =>
{
client.BaseAddress = new Uri("https+http://inventory");
});
Here, catalog and inventory are logical resource names resolved by Aspire. The application does not need to know which local ports or deployment addresses are used. This is especially valuable when moving from local development to containers or a cloud environment.
6. Passing Custom Environment Variables
Sometimes a process needs individual values instead of a complete connection string. For example, an executable resource may expect separate Redis host and port variables.
var worker = builder
.AddExecutable(
"report-worker",
"dotnet",
"../ReportWorker",
"ReportWorker.dll")
.WithReference(cache)
.WithEnvironment("WORKER_MODE", "development");
Endpoint properties can also be used to construct custom variables. Avoid writing passwords or tokens directly in source code. Use Aspire parameters, user secrets, or a secure secret mechanism provided by the deployment platform.
7. Common Mistakes
- Using only
WaitFor: A dependency may be ready, but the application still may not know how to connect to it. UseWithReferencewhen connection or endpoint information is required. - Changing resource names casually: Renaming
cachetoredis-cachechanges keys such asConnectionStrings__cache. Keep resource and configuration naming consistent. - Depending on fixed ports: Hard-coded ports can create conflicts. Use fixed ports only when an external system requires them.
- Assuming every connection string has the same format: Different integrations and client libraries may expect different formats. Inspect the actual configuration value during development.
- Sharing every resource with every service: Giving all services access to all databases and queues weakens ownership boundaries. Add only the references each service really needs.
- Ignoring host requirements for executables: A process started with
AddExecutablerequires its SDK, runtime, package manager, and operating-system dependencies to be installed locally.
8. Practice Exercise
Complete the following exercise:
- Add a Redis resource named
cacheand a RabbitMQ resource namedmessagingto the AppHost. - Give the Inventory service references to Redis and RabbitMQ only.
- Add a project reference from Order to Inventory and use
WaitForso Order waits for Inventory to become ready. - In the Order service, read
GetConnectionString("messaging")and throw a meaningful exception if it is missing. - Run the AppHost and inspect resource states and configuration values in the Aspire dashboard.
- Rename the Inventory resource and observe which service discovery and configuration values are affected.
The purpose is not merely to make the system run. You should be able to explain how the resource graph becomes application configuration.
Conclusion
This lesson introduced the practical resource model behind an Aspire AppHost. We registered .NET services as project resources, infrastructure components as container or integration resources, and host processes as executable resources. We also separated configuration references from startup dependencies: WithReference transfers connection and discovery information, while WaitFor manages readiness and startup order.
This distinction is important in distributed systems. Waiting for a service does not automatically tell an application how to connect to it, and receiving a connection string does not guarantee that the dependency is ready. Aspire lets us describe both concerns explicitly in the application model. In the next lesson, we will examine health checks, readiness, and resource startup behavior in more detail.
Sources
- https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/app-host-overview
- https://learn.microsoft.com/en-us/dotnet/aspire/app-host/executable-resources
- https://learn.microsoft.com/en-us/dotnet/aspire/service-discovery/overview
- https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/service-defaults
- https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/integrations-overview