Custom Resource Commands - Dynamic Command-Line Arguments #8502
Replies: 6 comments 2 replies
-
Beta Was this translation helpful? Give feedback.
-
This would also be handy for running end-to-end tests from the Aspire dashboard where we could have custom commands that trigger different test filters. |
Beta Was this translation helpful? Give feedback.
-
I also think this would be very useful. I have a couple of scenarios that I'm thinking of. I have a console application to run database migrations. I would like to start the app with different configurations. For example:
I have a console application to seed the database. I would like to start the app to seed with different options, like small, medium or large dataset. Also, for running different test sets as @rob-king-volpara mentioned could be useful. My current solution is to add the same project ((WithExplicitStart)) multiple times with different values for environment variables to the builder. It would be very nice if it was possible to add it only once but start it manually with different environment variables. I tried to do this with Commands, but could not figure out how to change the environment variables and launch the app. |
Beta Was this translation helpful? Give feedback.
-
The new Interaction Service looks to solve this (and then some) neatly - thanks! Will be giving it a go when I upgrade. |
Beta Was this translation helpful? Give feedback.
-
The interaction service looks really nice. I think I can use it to ask for information I need for running the resource. The piece I am missing is: how can i start an Aspire resource from a command using this information? So, for example, how can I ask: 'How many customers do you want to create?' and then start a resource (console app project that has a reference to the database and can seed data) and pass in the number of customers to create (e.g. as an environment variable). I read that in 9.4 there is a new ResourceCommandService that you can use to execute command. Maybe I'm looking for something similar to start/execute the resource I created the command on (and pass in some additional environment variables). |
Beta Was this translation helpful? Give feedback.
-
I made an example here using 9.4: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goal: add a custom resource command that conditionally sets a command-line argument before running the resource/executable.
this --migrate flag is set for a single run by the command

Using EF migrations in an Aspire world require some workarounds vs command-line tooling, with the most appealing & currently documented being a custom command-line app, managed via the dashboard.
With the new custom resource commands I wanted to add a single MyProject.Tools executable, with different in-UI commands for applying migrations, run certain seeding, etc. This was easy to achieve using a "set flag + run app" pair with a global bool:
from memory
but not very pleasant - requires multiple interactions and it's not clear what the current state is. By #6015 , there's not a built-in way that's exposed to end users.
The built-in commands (see here - very helpful for reference!) use ApplicationOrchestrator, which is internal - but with a simple signature, easy to get via reflection 😎.
Warning
This uses reflection and isn't very hardened - just sharing my workaround to what I suspect is a growing need + some thoughts on improvements.
Here's a gist that uses reflection to add a proxy class over ApplicationOrchestrator, with the
.Start()
method implemented (since that's all I needed). There are definitely better ways to do this, but with the baseline being "reflection of library's internal methods" I didn't put in too much effort. It's likely not very safe for long-running operations, and would need effort to prevent running multiple conflicting commands.To use:
builder.Services.AddApplicationOrchestratorProxy()
.WithArgs(c => /** conditionally call c.Args.Add() based on the flag or other logic **/)
.WithCommand()
to configure your custom command. This should set the global flag, resolve the proxy, start the app, and unset the flag when done..WithExplicitStart()
to prevent the app from running by default, if you want to require an explicit actionisHighlighted
, which moves the icon from the submenu to an (icon-only) option in the main bar - makes it one-click but requires a bit of duplication if you also want it in the list of text items.If successful, a new option will appear in your resource's section in the Aspire dashboard - both the main table, and in places like the log details toolbar. If configured as a highlighted option, it will be an icon next to the start/stop/logs icons.
Aspire team: in general, but especially with the EF migrations story strongly encouraging the "separate executable" scenarios, I feel it'd be helpful to have an official way to do one of:
A combo of 1 + 2 or 3 would solve this story in a fairly flexible way, without being overly tied to a specific resource type or encouraging loose global state. 2 or 3 alone would also work well but (unless I'm missing something) wouldn't feel as clean/safe for this use case.
I also played around with the app instead being a minimal server + HTTP commands, but didn't want to leave that running...
Please let me know if I missed an easier/safer option! And thanks for the tooling & platform, every update is easing modern dev pains.
Beta Was this translation helpful? Give feedback.
All reactions