Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 15, 2025

Problem

The TryMatchAgainstResources method in ResourceOutgoingPeerResolver was returning true on the first resource match, even when multiple resources had matching addresses. This could lead to ambiguous peer resolution where the method would return different results depending on the order of resources in the collection.

Solution

Updated TryMatchAgainstResources to:

  • Count all matching resources before returning a result
  • Return true only if exactly one resource matches
  • Return false if zero or multiple resources match (ambiguous case)
  • Set out parameters to null when returning false for multiple matches

Changes

Core Logic Update

// Before: returned immediately on first match
if (DoesAddressMatch(resourceAddress, peerAddress))
{
    name = ResourceViewModel.GetResourceName(resource, resources);
    resourceMatch = resource;
    return true; // ← Problem: returned without checking for other matches
}

// After: counts all matches before deciding
if (DoesAddressMatch(resourceAddress, peerAddress))
{
    if (foundResource is null)
    {
        foundResource = resource;
    }
    matchCount++;
    break; // No need to check other addresses for this resource
}
// Returns true only if matchCount == 1

Comprehensive Test Coverage

Added unit tests covering realistic scenarios:

  • SQL Server addresses: Multiple resources with localhost:1433 addresses
  • Redis addresses: Multiple resources with localhost:6379 addresses
  • SQL Server comma format: Resources matching localhost,1433 format
  • Mixed port formats: Resources with same logical address but different formats
  • Address transformation: Scenarios showing how 127.0.0.1localhost transformations work
  • Single match validation: Ensuring positive cases still work correctly

Test Results

  • ✅ All 24 ResourceOutgoingPeerResolver tests passing (17 existing + 7 new)
  • ✅ All 269 Dashboard tests passing (verified no regressions)
  • ✅ Covers realistic SQL Server and Redis endpoint scenarios

Files Changed

  • src/Aspire.Dashboard/Model/ResourceOutgoingPeerResolver.cs - Updated core matching logic
  • tests/Aspire.Dashboard.Tests/ResourceOutgoingPeerResolverTests.cs - Added comprehensive test scenarios

This fix ensures deterministic peer resolution behavior and prevents ambiguous matches from returning inconsistent results.

This pull request was created as a result of the following prompt from Copilot chat.

Update TryMatchAgainstResources to return false if multiple resources match, and update/add tests with realistic addresses. Only return true if exactly one match is found. If more than one resource matches, set out parameters to null and return false. Update or add unit tests in Aspire.Dashboard.Tests to cover: (1) no match, (2) single match, (3) multiple matches (ambiguous), using realistic resource addresses like SQL and Redis endpoints.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] TryMatchAgainstResources returns false if multiple resources match and adds realistic tests Fix TryMatchAgainstResources to return false for multiple resource matches Jul 15, 2025
@Copilot Copilot AI requested a review from davidfowl July 15, 2025 06:46
Copilot finished work on behalf of davidfowl July 15, 2025 06:46
@davidfowl
Copy link
Member

TryResolvePeerNameCore and TryResolvePeer are duplicated, remove TryResolvePeerNameCore

Copilot finished work on behalf of davidfowl July 15, 2025 06:53
Copy link
Contributor Author

Copilot AI commented Jul 15, 2025

TryResolvePeerNameCore and TryResolvePeer are duplicated, remove TryResolvePeerNameCore

Removed TryResolvePeerNameCore and consolidated the logic into TryResolvePeerCore. Both the public TryResolvePeer method and test helper now use the same core implementation, eliminating the duplication. 7de900f

Copilot finished work on behalf of davidfowl July 15, 2025 07:03
@davidfowl davidfowl marked this pull request as ready for review July 15, 2025 07:06
@davidfowl davidfowl requested a review from JamesNK July 15, 2025 07:07
Copy link
Member

@JamesNK JamesNK left a comment

Choose a reason for hiding this comment

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

One comment on comparing resources

@Copilot Copilot AI requested a review from JamesNK July 15, 2025 07:20
Copilot finished work on behalf of JamesNK July 15, 2025 07:20
@davidfowl davidfowl enabled auto-merge (squash) July 15, 2025 07:46
@davidfowl davidfowl merged commit 252130c into main Jul 15, 2025
276 checks passed
@davidfowl davidfowl deleted the copilot/fix-159024e0-6b44-4898-a840-f755828a293a branch July 15, 2025 07:53
@davidfowl
Copy link
Member

/backport to release/9.4

Copy link
Contributor

Started backporting to release/9.4: https://github.com/dotnet/aspire/actions/runs/16287515871

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants