DistributedApplication Properties
Hosting.IHost and IAsyncDisposable interfaces. public ResourceCommandService ResourceCommands { get; }Remarks
ResourceCommandService are: - Progamatically executing resource commands in a running AppHost.
- Unit or integration testing resource commands.
public ResourceNotificationService ResourceNotifications { get; }Remarks
ResourceNotificationService are: - Database seeding.
- Integration test readiness checks.
await app.ResourceNotifications.WaitForResourceHealthyAsync("postgres");await foreach ( var update in app.ResourceNotifications.WatchAsync(cancellationToken)){ Console.WriteLine( $"Resource {update .Resource .Name} state: {update .Snapshot .State? .Text}");}await app.ResourceNotifications.WaitForResourceAsync( "worker", KnownResourceStates.Running);// Wait for the database to be healthy before seedingawait 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();}IServiceProvider instance configured for the application. public IServiceProvider Services { get; }Remarks
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.