Add dependency-aware stop/start API (StopDependentsAsync / GetDependents) for programmatic resource lifecycle control
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
I am implementing a bacpac import command inside an Aspire AppHost. The import needs to stop any running project resources before importing, to prevent EF Core migrations from racing DacFx and causing schema ownership conflicts.
The current workaround is to stop **all** `ProjectResource` instances via `resourceCommandService.ExecuteCommandAsync(resource, "stop", ...)`. This has a problem:
It stops resources that are unrelated to the database being imported, slowing down the restart unnecessarily.
There is no first-class API in Aspire to query which resources depend on a given resource and combine that with the resource you want to stop, or to stop only a targeted dependency subgraph.
### Describe the solution you'd like
I would like a first-class, programmatic way to stop (and later restart) only the resources that directly or transitively depend on a given resource — ideally expressed entirely in C# without needing to issue raw command strings.
Concretely, two capabilities would cover the use-case:
**1. Dependency-aware stop/start helpers**
```csharp
// Stop every resource that has a reference/dependency on `targetDatabase`
await resourceCommandService.StopDependentsAsync(targetDatabase, cancellationToken);
// … do the import …
await resourceCommandService.StartDependentsAsync(targetDatabase, cancellationToken);
```
This would let the AppHost stop *only* the projects that actually use the database, leaving unrelated resources untouched and avoiding unnecessary downtime.
**2. Dependency graph query API**
If a full helper is too opinionated, exposing the dependency graph would also work:
```csharp
IEnumerable dependents = applicationModel.GetDependents(targetDatabase);
```
so that application code can filter, sort, and stop them in whatever order makes sense.
You can already use `distributedApplicationModel.Resources.OfType().` Perhapse a `ProjectResource` can have a list of recursive dependencies I could go through until I reach the resource I want?
### Additional context
See https://github.com/microsoft/aspire/issues/18282 for a bug related to this request
This request arose while implementing a dashboard command that imports a `.bacpac` file into a SQL Server database managed by Aspire. The full flow is:
1. Stop dependent project resources (to prevent EF Core migration conflicts with DacFx).
2. Drop the existing database.
3. Import the bacpac via DacFx.
4. Restart the dependent project resources.
Steps 1 and 4 currently enumerate `applicationModel.Resources.OfType()` and issue raw `"stop"`/`"start"` command strings. This is fragile: it stops unrelated projects.
A dependency-aware API would make this pattern clean, safe, and reusable for anyone who needs to perform maintenance operations on Aspire-managed resources.
Contributor guide
Assessment
This issue has not been assessed yet.