Fix aspire run to reconnect backchannel in watch mode instead of exiting #12079
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When running
aspire run
in watch mode, the CLI incorrectly exits when the backchannel disconnects. However, backchannel disconnections in watch mode typically occur becausedotnet watch
has restarted the apphost (e.g., due to a 'rude' change inProgram.cs
). The CLI should attempt to reconnect instead of terminating.Solution
This PR refactors the backchannel disconnect handling in
DotNetCliRunner
to detect watch mode and automatically attempt reconnection when a disconnect occurs:Watch Mode Behavior (New)
Non-Watch Mode Behavior (Unchanged)
Implementation Details
DotNetCliRunner.cs
changes:IsWatchMode
property toDotNetCliRunnerInvocationOptions
to track watch mode stateRunAsync()
to setIsWatchMode
based on thewatch
parameterStartBackchannelAsync()
to accept options and set up conditional disconnect handlersHandleBackchannelDisconnectInWatchModeAsync()
method containing the reconnection logicTest coverage:
IsWatchMode
property is correctly set in both watch and non-watch scenariosExample Log Output
Successful reconnection in watch mode:
Non-watch mode (existing behavior):
Testing
Aspire.Cli.Tests
passTechnical Notes
Task.Run
for the disconnect handler sinceEventHandler<JsonRpcDisconnectedEventArgs>
is synchronous but reconnection logic is asyncIAppHostBackchannel
instance for each reconnection attempt (required by interface design)Fixes scenario where
dotnet watch
restarts cause CLI to exit unnecessarily.Original prompt
This pull request was created as a result of the following prompt from Copilot chat.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.