As a .NET application grows, its architecture often becomes more complicated. A solution that initially contains one API and one database may eventually include multiple APIs, background workers, frontend applications, message brokers, caches, and databases. Managing all these resources through a single .NET Aspire AppHost can become difficult.
The AppHost is responsible for describing the resources in a distributed application and defining how they work together. It also helps developers run and debug those resources during local development. However, a large solution does not always need one AppHost to manage every project.
Using multiple AppHosts can help divide a large solution into smaller, independently manageable application groups. This approach can improve development workflows, reduce orchestration complexity, and allow teams to work on different parts of a system without starting every service.
In this article, we will explore when multiple Aspire AppHosts are useful, how to organize them in a large .NET solution, and how to manage dependencies and shared infrastructure between independently orchestrated applications.
Understanding the Role of an Aspire AppHost
A .NET Aspire AppHost is the entry point for defining and orchestrating a distributed application during development.
Instead of manually starting each service, database, cache, and other dependency, developers describe the application resources in the AppHost. Aspire uses this application model to manage local startup, resource dependencies, service discovery, configuration injection, and diagnostics.
For example, consider a typical application containing:
- An ASP.NET Core API.
- A React frontend.
- A background worker.
- A PostgreSQL database.
- A Redis cache.
A single AppHost can define all these resources and their relationships.
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres")
.WithDataVolume();
var database = postgres.AddDatabase("catalog");
var cache = builder.AddRedis("cache");
var api = builder.AddProject<Projects.Catalog_Api>("catalog-api")
.WithReference(database)
.WithReference(cache)
.WaitFor(database);
builder.AddProject<Projects.Catalog_Web>("catalog-web")
.WithReference(api);
builder.AddProject<Projects.Catalog_Worker>("catalog-worker")
.WithReference(database)
.WithReference(cache);
builder.Build().Run();
This works well for a relatively small application. However, imagine that the same solution grows to include customer management, payments, reporting, notifications, inventory, and analytics services. Each service may have its own dependencies and development requirements.
At that point, managing every resource through one large AppHost can become inconvenient.
Why Use Multiple AppHosts?
Multiple AppHosts allow developers to organize orchestration around application boundaries rather than forcing every resource into one application model.
Consider an e-commerce platform with the following components:
Application area | Services and dependencies |
|---|
Catalog | Catalog API, catalog database, Redis |
Orders | Order API, order worker, order database |
Payments | Payment API, payment database |
Notifications | Notification worker, message broker |
Analytics | Reporting API, analytics database, processing worker |
If every resource is registered in one AppHost, even a developer working only on the catalog may need to start unrelated services.
Instead, the solution can have separate AppHosts:
- Catalog.AppHost manages catalog-related resources.
- Orders.AppHost manages order-related resources.
- Payments.AppHost manages payment-related resources.
- Analytics.AppHost manages reporting and analytics resources.
Each AppHost describes a smaller distributed application.
This separation is particularly useful when teams own different application areas or when some services require considerably more infrastructure than others.
Organizing a Large Solution With Multiple AppHosts
A project-based Aspire solution can contain multiple AppHost projects. Each AppHost references the application projects it needs to orchestrate.
Consider the following solution structure:
CommercePlatform/
│
├── CommercePlatform.sln
│
├── Catalog/
│ ├── Catalog.Api/
│ ├── Catalog.Worker/
│ ├── Catalog.AppHost/
│ └── Catalog.ServiceDefaults/
│
├── Orders/
│ ├── Orders.Api/
│ ├── Orders.Worker/
│ └── Orders.AppHost/
│
├── Payments/
│ ├── Payments.Api/
│ └── Payments.AppHost/
│
├── Analytics/
│ ├── Analytics.Api/
│ ├── Analytics.Worker/
│ └── Analytics.AppHost/
│
└── Shared/
├── Common.Contracts/
└── Common.Infrastructure/
This structure groups related projects together while keeping the solution in one repository.
Each AppHost has its own project references and resource definitions. For example, Catalog.AppHost references Catalog.Api and Catalog.Worker, while Orders.AppHost references Orders.Api and Orders.Worker.
You can add these references through the .NET CLI:
dotnet add Catalog.AppHost reference Catalog.Api
dotnet add Catalog.AppHost reference Catalog.Worker
dotnet add Orders.AppHost reference Orders.Api
dotnet add Orders.AppHost reference Orders.Worker
The project-based AppHost uses generated project metadata to provide strongly typed references through the Projects namespace.
This makes it easier to maintain separate orchestration definitions while keeping application projects inside the same solution.
Creating a Separate AppHost for Each Application Area
Let's implement two independent AppHosts for a commerce application.
The first AppHost manages the catalog API and its database.
Catalog.AppHost/Program.cs:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("catalog-postgres")
.WithDataVolume();
var catalogDb = postgres.AddDatabase("catalog-db");
builder.AddProject<Projects.Catalog_Api>("catalog-api")
.WithReference(catalogDb)
.WaitFor(catalogDb);
builder.Build().Run();
The second AppHost manages the order API and its background worker.
Orders.AppHost/Program.cs:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("orders-postgres")
.WithDataVolume();
var ordersDb = postgres.AddDatabase("orders-db");
var api = builder.AddProject<Projects.Orders_Api>("orders-api")
.WithReference(ordersDb)
.WaitFor(ordersDb);
builder.AddProject<Projects.Orders_Worker>("orders-worker")
.WithReference(ordersDb)
.WaitFor(ordersDb);
builder.Build().Run();
Now the catalog and order application areas can be started independently.
A developer working on catalog functionality can run Catalog.AppHost without starting the order worker or its database. Similarly, an order developer can run Orders.AppHost without starting the catalog resources.
This reduces unnecessary local resource usage and makes the development environment easier to manage.
Managing Dependencies Between Different AppHosts
One important detail is that separate AppHosts maintain separate application models.
For example, when Catalog.AppHost defines a catalog API and Orders.AppHost defines an order API, the two AppHosts do not automatically become part of one shared orchestration graph.
A resource reference created in one AppHost is not automatically available as a resource reference in another AppHost.
This means that developers must explicitly decide how services communicate across these boundaries.
Consider a scenario where the order service needs to call the catalog API.
When both services are managed by the same AppHost, Aspire can establish a resource reference:
var catalogApi = builder.AddProject<Projects.Catalog_Api>("catalog-api");
builder.AddProject<Projects.Orders_Api>("orders-api")
.WithReference(catalogApi);
However, when the catalog and order services are managed by separate AppHosts, this direct resource reference is not available across those independent application models.
In that situation, the order service should use an explicitly configured endpoint for the catalog API.
For example, the Orders.AppHost can provide the catalog API URL through an environment variable:
var builder = DistributedApplication.CreateBuilder(args);
var catalogApiUrl = builder.Configuration["Services:CatalogApi"]
?? throw new InvalidOperationException(
"Catalog API URL is not configured.");
builder.AddProject<Projects.Orders_Api>("orders-api")
.WithEnvironment("Services__CatalogApi", catalogApiUrl);
builder.Build().Run();
The Orders API can read that value through its configuration system.
var catalogApiUrl =
builder.Configuration["Services:CatalogApi"];
In a real deployment, the URL should come from the appropriate environment-specific configuration provider or service discovery infrastructure.
For local development, the catalog AppHost and order AppHost may run on different dynamically assigned ports. Therefore, developers should use a stable local endpoint configuration strategy rather than assuming a fixed port.
The key principle is to treat communication between separate AppHosts as communication between independently managed applications.
Sharing Infrastructure Between Multiple AppHosts
Another important decision involves databases, caches, and message brokers.
Suppose both the catalog and order services need access to the same Redis instance. There are two possible approaches.
The first approach is to define a Redis resource separately in each AppHost. This creates independent Redis resources for local development.
Catalog.AppHost:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("catalog-cache");
builder.AddProject<Projects.Catalog_Api>("catalog-api")
.WithReference(cache);
builder.Build().Run();
Orders.AppHost:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("orders-cache");
builder.AddProject<Projects.Orders_Api>("orders-api")
.WithReference(cache);
builder.Build().Run();
This is useful when the applications should be developed and tested independently. However, these are two separate Redis instances, not one shared cache.
The second approach is to use a shared external infrastructure resource. Both AppHosts can receive the same Redis endpoint through their respective environment configurations.
For example, the Catalog.AppHost can use:
var builder = DistributedApplication.CreateBuilder(args);
var cacheConnection =
builder.Configuration["SharedInfrastructure:Redis"]
?? throw new InvalidOperationException(
"Redis connection is missing.");
builder.AddProject<Projects.Catalog_Api>("catalog-api")
.WithEnvironment(
"ConnectionStrings__shared-cache",
cacheConnection);
builder.Build().Run();
The Orders.AppHost can receive the same connection value from its own configuration source.
The application projects then use the appropriate Redis client configuration.
This approach is useful when multiple independently orchestrated applications need to connect to the same infrastructure.
However, it also means that the shared resource's lifecycle is managed outside those individual AppHosts. Teams must coordinate its availability, configuration, and cleanup.
Running Multiple AppHosts During Development
Multiple AppHosts are most useful when developers can start only the application area they need.
For example, a developer working on the catalog API can select Catalog.AppHost as the startup project in Visual Studio.
Another developer can select Orders.AppHost.
The same workflow can be performed using the Aspire CLI, provided the appropriate AppHost is selected for the operation.
The important point is that each AppHost is an independent startup entry point. Running one does not automatically start the resources declared in another.
If a developer needs to test communication between catalog and orders, both AppHosts must be started separately, and their services must be configured to communicate through reachable endpoints.
This creates a useful distinction between two development scenarios:
Development scenario | Recommended approach |
|---|
Working on one application area | Run its dedicated AppHost |
Testing two independent services together | Run both AppHosts and configure their endpoints |
Testing the complete distributed system | Use a dedicated integration or full-system orchestration setup |
Testing one service with a shared database | Use an AppHost that manages the service and its required database |
A dedicated integration environment can be useful when developers need to test the complete system regularly. It should be designed intentionally rather than assuming that multiple AppHosts automatically combine into one orchestration environment.
Common Mistakes When Using Multiple AppHosts
Assuming resource references work across AppHosts
WithReference establishes a relationship inside an AppHost's application model. It does not create an automatic connection between resources declared in different AppHosts.
Use explicit endpoint configuration or an appropriate external service discovery mechanism for cross-application communication.
Defining shared infrastructure multiple times
Registering Redis or PostgreSQL in two AppHosts creates separate local resources. It does not mean both applications share the same database or cache.
Decide whether infrastructure should be independent or externally shared before defining the resources.
Using fixed ports without coordination
Two AppHosts may try to start services on the same local port. This can lead to startup failures and confusing debugging sessions.
Allow Aspire to allocate endpoints dynamically where possible. If fixed ports are required, define a consistent port-allocation strategy across the solution.
Creating too many AppHosts
A separate AppHost for every individual service is not always necessary. Excessive separation can introduce additional configuration, startup, and coordination overhead.
Group resources according to meaningful application boundaries and actual development requirements.
Treating AppHosts as production service managers
Aspire AppHosts are primarily designed for development-time orchestration. They help developers run, connect, and observe distributed application resources locally.
They should not be treated as production process supervisors or as a replacement for a production deployment platform.
Best Practices for Large .NET Aspire Solutions
When introducing multiple AppHosts, keep the architecture intentional.
- Organize AppHosts around business capabilities or independently developed application areas.
- Keep resource definitions close to the services that own them.
- Use separate databases or caches when independent development and testing require isolation.
- Use explicit configuration for communication between independently orchestrated services.
- Avoid fixed local ports unless there is a clear requirement.
- Share reusable application contracts and libraries without creating unnecessary dependencies between services.
- Maintain a dedicated way to test important cross-service workflows.
- Keep AppHost configuration focused on orchestration rather than placing all business logic inside it.
- Document which team or application owns each shared infrastructure resource.
These practices help prevent a large solution from becoming a collection of disconnected development environments.
Advantages and Disadvantages
Multiple AppHosts provide several advantages:
- Developers can run only the services they need.
- Application boundaries become easier to understand.
- Teams can maintain independent development workflows.
- Large AppHost definitions become smaller and more manageable.
- Local infrastructure can be isolated for individual application areas.
- Debugging unrelated services becomes less necessary during routine development.
However, there are also trade-offs:
- Cross-AppHost communication requires explicit endpoint or discovery configuration.
- Shared infrastructure needs additional coordination.
- Developers may need to start multiple AppHosts for integration testing.
- Port conflicts and environment configuration require attention.
- Maintaining several orchestration projects introduces additional code and ownership responsibilities.
Multiple AppHosts are most useful when the benefits of independent orchestration outweigh the additional coordination required between application areas.
Summary
Running multiple .NET Aspire AppHosts is a practical approach for organizing large .NET solutions. Instead of defining every API, worker, database, and cache in one large AppHost, developers can divide orchestration into smaller groups based on business capabilities and application boundaries.
Each AppHost maintains its own resource model, dependencies, and startup workflow. This makes it easier to work on individual application areas without starting the entire distributed system.
However, separate AppHosts do not automatically share resource references or infrastructure. Services communicating across AppHost boundaries need explicit endpoint configuration or an appropriate external service discovery mechanism. Shared databases, caches, and message brokers also require a clear ownership and lifecycle strategy.
The best design is not necessarily the one with the most AppHosts. It is the one that keeps development workflows simple, service boundaries clear, and cross-service communication predictable.
Join the conversation! Your thoughts help the community grow.