microsoft / microsoft/aspire

`AfterResourcesCreatedEvent` is inconsistently fired

Open
#7,009 5 comments 1 reaction 1 assignee Assigned to @Copilot View on GitHub
area-app-model breaking-change
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

I'm finding the logic for when the `AfterResourcesCreatedEvent` is fired to be very confusing. Take the following code for example:

```cs
var builder = DistributedApplication.CreateBuilder(args);

builder.Services.AddHealthChecks().AddCheck("alwaysUnhealthy", () => HealthCheckResult.Unhealthy());

var one = builder.AddContainer("one", "nginx").WithHealthCheck("alwaysUnhealthy");
var two = builder.AddContainer("two", "nginx");

//two.WaitFor(one);

builder.Eventing.Subscribe((evt, ct) =>
{
Console.WriteLine("AfterResourcesCreatedEvent");
Console.Beep();
return Task.CompletedTask;
});

builder.Build().Run();
```
Run this whilst **docker is stopped**, no resoruces are stated but the `AfterResourcesCreatedEvent` does get fired.

![Image](https://github.com/user-attachments/assets/dba7d3fd-fb8d-41e5-adff-3f21743431cd)

However if you comment out the `//two.WaitFor(one);` line above and run the app (whilst **docker is still not running**), then the `AfterResourcesCreatedEvent` does not get fired.
Image

The issue here seems to be that when `WaitFor` is used, the `AfterResourcesCreatedEvent` is blocked until all health checks become healthy. However the runtime availability is handled by a different flow which does not block `AfterResourcesCreatedEvent`.

From my point of view, both the above are essentially the same in both cases the containers are waiting on something else before they can start, and so I'd expect either both cases to raise `AfterResourcesCreatedEvent` or I'd neither to raise the event.

![Image](https://github.com/user-attachments/assets/b6291e61-e71f-462b-aad6-86df5f13fe60)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.