DistributedApplication.ResourceNotifications Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the service for monitoring and responding to resource state changes in the distributed application.
public Aspire.Hosting.ApplicationModel.ResourceNotificationService ResourceNotifications { get; }
member this.ResourceNotifications : Aspire.Hosting.ApplicationModel.ResourceNotificationService
Public ReadOnly Property ResourceNotifications As ResourceNotificationService
Property Value
Examples
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();
}
Remarks
Two common use cases for the ResourceNotificationService are: