Skip to content
Docs Try Aspire

DistributedApplication Properties

Class Properties 3 members
Represents a distributed application that implements the Hosting.IHost and IAsyncDisposable interfaces.
Gets the service for executing resource commands.
public ResourceCommandService ResourceCommands { get; }
Two common use cases for the ResourceCommandService are:
Gets the service for monitoring and responding to resource state changes in the distributed application.
public ResourceNotificationService ResourceNotifications { get; }
Two common use cases for the ResourceNotificationService are:
Wait for resource readiness:
await app.ResourceNotifications.WaitForResourceHealthyAsync("postgres");
Monitor state changes:
await foreach (
var update in app.ResourceNotifications.WatchAsync(cancellationToken))
{
Console.WriteLine(
$"Resource {update
.Resource
.Name} state: {update
.Snapshot
.State?
.Text}");
}
Wait for a specific state:
await app.ResourceNotifications.WaitForResourceAsync(
"worker",
KnownResourceStates.Running);
Seed a database once it becomes available:
// Wait for the database to be healthy before seeding
await app.ResourceNotifications.WaitForResourceHealthyAsync("postgres");
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(
);
await dbContext.Database.EnsureCreatedAsync();
if (!dbContext.Products.Any())
{
await dbContext.Products.AddRangeAsync(
[
new Product { Name = "Product 1", Price = 10.99m },
new Product { Name = "Product 2", Price = 20.99m }
]);
await dbContext.SaveChangesAsync();
}
Services Section titled Services IServiceProvider
Gets the IServiceProvider instance configured for the application.
public IServiceProvider Services { get; }

The DistributedApplication is an Hosting.IHost implementation and as such exposes a DistributedApplication.Services property which allows developers to get services from the dependency injection container after DistributedApplication instance has been built using the IDistributedApplicationBuilder.Build method.

To add services to the dependency injection container developers should use the IDistributedApplicationBuilder.Services property to access the DependencyInjection.IServiceCollection instance.