Start, Stop Resource Programmatically
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 want to Start, Stop Resources Programmatically. Can you add this feature?
No, this api will never be public. Your time is better spent asking for specific features and the team will decide how best those should be expose.
Exposing APIs ties our hands and stops us from doing refactoring (this type didn’t exist before 9.1, and it’ll probably get rewritten again at some point)
No, this api will never be public. Your time is better spent asking for specific features and the team will decide how best those should be expose.
Exposing APIs ties our hands and stops us from doing refactoring (this type didn’t exist before 9.1, and it’ll probably get rewritten again at some point)
Thanks for response. I updated the issue.
I was thinking about this recently in the context of looking at dev tunnels too (as I'd likely want to model the invocation of the devtunnels CLI as invisible resources that are started via resource commands). We have APIs like ResourceNotificationService already so perhaps we introduce a new service like ResourceLifetimeService that has StartResourceAsync, StopResourceAsync, RestartResourceAsync, etc. on it.
As a workaround that works right now, you can get the start and stop command from the resources annotations. For example
var startCommand = resourceBuilder
.Resource
.Annotations
.OfType<ResourceCommandAnnotation>()
.First(c => c.Name == "resource-start");
var stopCommand = resourceBuilder
.Resource
.Annotations
.OfType<ResourceCommandAnnotation>()
.First(c => c.Name == "resource-stop");
await stopCommand.ExecuteCommand(context);
await startCommand.ExecuteCommand(context);
Top tier hacking @jack775544
I made a custom command for myself to stop a project, run dotnet build on the command line and then start it again after it was done. So I had to dig around to find out how to start and stop a project for that.
It would be nice if there was appropriate BeforeResourceStartedEvent, AfterResourceStartedEvent, BeforeResourceStoppedEvent, AfterResourceStoppedEvent events linked to this.
Not sure what you mean. The built in resources fire events appropriately (or they are suppose me to).
Not sure what you mean. The built in resources fire events appropriately (or they are suppose me to).
There are state change notifications, but not resource events (Only BeforeResourceStartedEvent) that match up with lifecycle events.
There are apphost lifecycle events and IDistributedApplicationLifecycleHook but not similar for a resource. If you stop and start a resource, there are no stop events (that I can see), and the BeforeResourceStartedEvent does not trigger (again) if you stop and start or restart.
I think BeforeResourceStartedEvent does trigger on each start of the resource (pretty sure I hit this myself). But I hear what you're saying RE more resource events rather than having to consume notifications and process state updates.
Right afaik BeforeResourceStartedEvent does fire. That’s the only event we have right now that makes sense to re-trigger. The global ones fire once and the per resource ones should figure when each resource changes.
The global ones won’t fire again nor should they (it would cause too many inconsistencies!
My testing shows that it didn't fire in 9.0 but does re-fire in 9.2. I wouldn't expect the global events to fire, but we are missing easy access to stop/start events per resource. I don't want to take this issue too far off topic though.
I think there are 4 issues asking for an API to invoke commands/start/stop programmatically. The need is well understood.
Is there an option to not run a project on Aspire start?
The only option you have today is to use WithExplicitStart. Maybe follow this issue https://github.com/dotnet/aspire/issues/303?
Closing in favor of https://github.com/dotnet/aspire/issues/9804