Skip to content

Use GetRequiredService for DistributedApplicationModel, extract ResolveResourceIds helpers, and improve test coverage#16320

Merged
JamesNK merged 6 commits intomainfrom
fix/16246-resolve-resource-ids-refactor
Apr 20, 2026
Merged

Use GetRequiredService for DistributedApplicationModel, extract ResolveResourceIds helpers, and improve test coverage#16320
JamesNK merged 6 commits intomainfrom
fix/16246-resolve-resource-ids-refactor

Conversation

@JamesNK
Copy link
Copy Markdown
Member

@JamesNK JamesNK commented Apr 20, 2026

Description

Cleans up AuxiliaryBackchannelRpcTarget and improves its test coverage.

Use GetRequiredService for DistributedApplicationModel

DistributedApplicationModel is always registered by the hosting infrastructure. Several methods used GetService with defensive null checks that silently degraded behavior (e.g., returning empty results or falling back to unresolved names). Replaced all GetService<DistributedApplicationModel>() calls with GetRequiredService<DistributedApplicationModel>() and removed the dead null-handling code in:

  • ResolveWaitTarget — removed null fallback that returned an unresolved WaitResourceTarget
  • ResolveDisplayName — changed parameter from DistributedApplicationModel? to DistributedApplicationModel, removed null guard
  • GetResourceSnapshotsAsync — removed early empty return
  • CallResourceMcpToolAsync — removed manual null check + InvalidOperationException

Extract ResolveResourceIds and ResolveAllResourceIds helpers

Extracted the inline resource-name resolution logic from GetResourceLogsAsync into two reusable private static methods:

  • ResolveResourceIds(appModel, resourceName) — resolves a logical name or instance name to matching resource IDs
  • ResolveAllResourceIds(appModel) — returns all resolved resource IDs

Improve test coverage

  • Removed GetResourceSnapshotsAsync_ReturnsEmptyList_WhenAppModelIsNull (no longer applicable)
  • Added DefaultTimeout() to all await calls across all tests
  • Added WaitForResourceAsync_ReturnsNotFound_WhenResourceDoesNotExist — verifies not-found for unknown resource
  • Added WaitForResourceAsync_ReturnsNotFound_ForBadInstanceName — verifies not-found for a non-existent instance name
  • Expanded GetResourceLogsAsync_ReturnsLogsFromReplicas to verify logs from unrelated resources are excluded
  • Expanded GetResourceLogsAsync_ReturnsLogsForSingleReplica_WhenResolvedInstanceNameIsPassed to verify bad instance names return empty results

Fixes #16246

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?

Copilot AI review requested due to automatic review settings April 20, 2026 03:45
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 20, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 16320

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 16320"

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors resource-name-to-instance-id resolution into shared helper methods within AuxiliaryBackchannelRpcTarget and expands the backchannel tests to use consistent timeouts and cover additional log-filtering scenarios (including replica vs unrelated resources).

Changes:

  • Extract ResolveResourceIds / ResolveAllResourceIds from GetResourceLogsAsync and reuse in WaitForResourceAsync’s “resource exists” pre-check.
  • Update GetResourceLogsAsync to use the new helpers and add extra replica/unrelated-resource assertions in tests.
  • Standardize test awaits to use DefaultTimeout().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Aspire.Hosting/Backchannel/AuxiliaryBackchannelRpcTarget.cs Introduces shared resource-id resolution helpers and applies them to log streaming and the wait pre-check.
tests/Aspire.Hosting.Tests/Backchannel/AuxiliaryBackchannelRpcTargetTests.cs Replaces ad-hoc waits with DefaultTimeout() and adds stronger log filtering/negative-case assertions.

Comment thread src/Aspire.Hosting/Backchannel/AuxiliaryBackchannelRpcTarget.cs Outdated
JamesNK added 3 commits April 20, 2026 12:36
… matching

- Extract ResolveResourceIds/ResolveAllResourceIds from GetResourceLogsAsync
- Fix WaitForResourceAsync to accept resolved instance names (e.g. apiservice-abc123)
- Add DefaultTimeout to all await calls in AuxiliaryBackchannelRpcTargetTests
- Add test coverage: verify logs from other resources are excluded, bad instance IDs return empty
- Add ResolveLogicalResourceName to map instance names back to Resource.Name
- Fix WaitForResourceAsync to pass logical name to ResourceNotificationService
- Add tests: logical name, not-found, resolved instance name, bad instance name
@JamesNK JamesNK force-pushed the fix/16246-resolve-resource-ids-refactor branch from a7210bf to 358b5c0 Compare April 20, 2026 04:40
@JamesNK JamesNK changed the title Extract ResolveResourceIds and fix WaitForResourceAsync resolved name matching Use GetRequiredService for DistributedApplicationModel, extract ResolveResourceIds helpers, and improve test coverage Apr 20, 2026
@JamesNK JamesNK enabled auto-merge (squash) April 20, 2026 05:30
@JamesNK JamesNK merged commit 537d2a6 into main Apr 20, 2026
561 of 565 checks passed
@github-actions github-actions Bot added this to the 13.3 milestone Apr 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🎬 CLI E2E Test Recordings — 72 recordings uploaded (commit 6edcb5c)

View recordings
Test Recording
AddPackageInteractiveWhileAppHostRunningDetached ▶️ View Recording
AddPackageWhileAppHostRunningDetached ▶️ View Recording
AgentCommands_AllHelpOutputs_AreCorrect ▶️ View Recording
AgentInitCommand_DefaultSelection_InstallsSkillOnly ▶️ View Recording
AgentInitCommand_MigratesDeprecatedConfig ▶️ View Recording
AspireAddPackageVersionToDirectoryPackagesProps ▶️ View Recording
AspireUpdateRemovesAppHostPackageVersionFromDirectoryPackagesProps ▶️ View Recording
Banner_DisplayedOnFirstRun ▶️ View Recording
Banner_DisplayedWithExplicitFlag ▶️ View Recording
Banner_NotDisplayedWithNoLogoFlag ▶️ View Recording
CertificatesClean_RemovesCertificates ▶️ View Recording
CertificatesTrust_WithNoCert_CreatesAndTrustsCertificate ▶️ View Recording
CertificatesTrust_WithUntrustedCert_TrustsCertificate ▶️ View Recording
ConfigSetGet_CreatesNestedJsonFormat ▶️ View Recording
CreateAndRunAspireStarterProject ▶️ View Recording
CreateAndRunAspireStarterProjectWithBundle ▶️ View Recording
CreateAndRunEmptyAppHostProject ▶️ View Recording
CreateAndRunJavaEmptyAppHostProject ▶️ View Recording
CreateAndRunJsReactProject ▶️ View Recording
CreateAndRunPythonReactProject ▶️ View Recording
CreateAndRunTypeScriptEmptyAppHostProject ▶️ View Recording
CreateAndRunTypeScriptStarterProject ▶️ View Recording
CreateJavaAppHostWithViteApp ▶️ View Recording
CreateTypeScriptAppHostWithViteApp ▶️ View Recording
DashboardRunWithOtelTracesReturnsNoTraces ▶️ View Recording
DeployK8sBasicApiService ▶️ View Recording
DeployK8sWithGarnet ▶️ View Recording
DeployK8sWithMongoDB ▶️ View Recording
DeployK8sWithMySql ▶️ View Recording
DeployK8sWithPostgres ▶️ View Recording
DeployK8sWithRabbitMQ ▶️ View Recording
DeployK8sWithRedis ▶️ View Recording
DeployK8sWithSqlServer ▶️ View Recording
DeployK8sWithValkey ▶️ View Recording
DeployTypeScriptAppToKubernetes ▶️ View Recording
DescribeCommandResolvesReplicaNames ▶️ View Recording
DescribeCommandShowsRunningResources ▶️ View Recording
DetachFormatJsonProducesValidJson ▶️ View Recording
DetachFormatJsonProducesValidJsonWhenRestartingExistingInstance ▶️ View Recording
DoListStepsShowsPipelineSteps ▶️ View Recording
DoctorCommand_DetectsDeprecatedAgentConfig ▶️ View Recording
DoctorCommand_WithSslCertDir_ShowsTrusted ▶️ View Recording
DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ▶️ View Recording
GlobalMigration_HandlesCommentsAndTrailingCommas ▶️ View Recording
GlobalMigration_HandlesMalformedLegacyJson ▶️ View Recording
GlobalMigration_PreservesAllValueTypes ▶️ View Recording
GlobalMigration_SkipsWhenNewConfigExists ▶️ View Recording
GlobalSettings_MigratedFromLegacyFormat ▶️ View Recording
InitTypeScriptAppHost_AugmentsExistingViteRepoAtRoot ▶️ View Recording
InvalidAppHostPathWithComments_IsHealedOnRun ▶️ View Recording
LegacySettingsMigration_AdjustsRelativeAppHostPath ▶️ View Recording
LogsCommandShowsResourceLogs ▶️ View Recording
OtelLogsReturnsStructuredLogsFromStarterApp ▶️ View Recording
PsCommandListsRunningAppHost ▶️ View Recording
PsFormatJsonOutputsOnlyJsonToStdout ▶️ View Recording
PublishWithConfigureEnvFileUpdatesEnvOutput ▶️ View Recording
PublishWithDockerComposeServiceCallbackSucceeds ▶️ View Recording
PublishWithoutOutputPathUsesAppHostDirectoryDefault ▶️ View Recording
RestoreGeneratesSdkFiles ▶️ View Recording
RestoreRefreshesGeneratedSdkAfterAddingIntegration ▶️ View Recording
RestoreSupportsConfigOnlyHelperPackageAndCrossPackageTypes ▶️ View Recording
RunFromParentDirectory_UsesExistingConfigNearAppHost ▶️ View Recording
SecretCrudOnDotNetAppHost ▶️ View Recording
SecretCrudOnTypeScriptAppHost ▶️ View Recording
StagingChannel_ConfigureAndVerifySettings_ThenSwitchChannels ▶️ View Recording
StartAndWaitForTypeScriptSqlServerAppHostWithNativeAssets ▶️ View Recording
StopAllAppHostsFromAppHostDirectory ▶️ View Recording
StopAllAppHostsFromUnrelatedDirectory ▶️ View Recording
StopNonInteractiveMultipleAppHostsShowsError ▶️ View Recording
StopNonInteractiveSingleAppHost ▶️ View Recording
StopWithNoRunningAppHostExitsSuccessfully ▶️ View Recording
UnAwaitedChainsCompileWithAutoResolvePromises ▶️ View Recording

📹 Recordings uploaded automatically from CI run #24649525047

@JamesNK JamesNK deleted the fix/16246-resolve-resource-ids-refactor branch April 20, 2026 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP list_console_logs returns empty for Running+Healthy resources

3 participants