DistributedApplication class and IDistributedApplicationBuilder interface are the foundation of the .NET Aspire application model. They provide the entry point for defining distributed applications and orchestrating resources.
DistributedApplication
Represents a distributed application that implementsIHost and IAsyncDisposable. This is the main entry point for running .NET Aspire applications.
Creating a Distributed Application
Use the static factory methods to create an application builder:Static Methods
Creates a new builder instance with no arguments. Use this only when not deploying with tools.
Creates a builder with command-line arguments. This is the recommended approach as it enables deployment tool support.Parameters:
args- Command-line arguments passed to the application
Creates a builder with full configuration options.Parameters:
options- Configuration options including:Args- Command-line argumentsDisableDashboard- Disable the Aspire dashboardAllowUnsecuredTransport- Allow unsecured communicationContainerRegistryOverride- Override the container registryDashboardApplicationName- Custom application name for the dashboard
Instance Properties
Gets the service provider configured for the application. Use this to resolve services after the application is built.
Gets the service for monitoring and responding to resource state changes. Useful for:
- Database seeding
- Integration test readiness checks
- Monitoring resource health
Gets the service for executing resource commands programmatically or in tests.
Methods
Runs the application and blocks until shutdown is triggered (Ctrl-C, debugger detach, or dashboard browser close).
Runs the application asynchronously and returns a task that completes when shutdown is triggered.Parameters:
cancellationToken- Optional cancellation token
Starts the application without blocking. Returns a task that completes when startup is finished.
Stops the application gracefully.
Resource Monitoring Example
Wait for Resource Health
IDistributedApplicationBuilder
The builder interface for configuring a distributed application before it is built.Properties
Gets the hosting environment information (Development, Production, etc.).
Gets the configuration manager for accessing application configuration.
Gets the service collection for registering dependency injection services.
Gets the collection of resources defined in the application.
Gets the eventing system for subscribing to application lifecycle events.
Gets the execution context indicating whether the app is in Run, Publish, or Inspect mode.
Gets the directory path of the app host project.
Methods
Adds a resource to the application and returns a builder for further configuration.Parameters:
resource- The resource instance to add
DistributedApplicationException if a resource with the same name already existsCreates a resource builder for an existing resource without adding it to the application.Parameters:
resource- The resource instance
Builds the configured application and returns a
DistributedApplication instance ready to run.Configuration Example
Service Registration
Execution Modes
Aspire applications run in different modes depending on how they’re launched:Run Mode
Run Mode
The normal mode when running the application locally. The app host orchestrates all resources and runs until shutdown.
Publish Mode
Publish Mode
Activated when generating manifests for deployment tools. The app runs briefly to generate configuration.
Inspect Mode
Inspect Mode
Used by tooling to inspect the application model without running resources.
Best Practices
Pass Command-Line Args
Always pass
args to CreateBuilder() to enable deployment tool support and proper configuration.Dispose Properly in Tests
Call
DisposeAsync() on the application in test scenarios to clean up file watchers and resources.Use Resource Notifications
Monitor resource health before performing operations like database seeding or integration tests.
Configure Services Early
Register custom services in
builder.Services before calling Build().Related APIs
- IResourceBuilder - Building and configuring individual resources
- IResource - Resource interfaces and contracts
- Hosting Extensions - Extension methods for adding resources