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; }Remarks
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; }Remarks
Two common use cases for the Monitor state changes: Wait for a specific state: Seed a database once it becomes available:
ResourceNotificationService are: 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();} Gets the
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.