Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix more tests
  • Loading branch information
danegsta authored and github-actions committed Oct 31, 2025
commit f1edd09bffd2c86b2883a7c466355a4595f41ee7
15 changes: 5 additions & 10 deletions src/Aspire.Hosting/ApplicationModel/EndpointReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class EndpointReference : IManifestExpressionProvider, IValueProvi
// A reference to the endpoint annotation if it exists.
private EndpointAnnotation? _endpointAnnotation;
private bool? _isAllocated;
private readonly NetworkIdentifier _contextNetworkID;
private readonly NetworkIdentifier? _contextNetworkID;

/// <summary>
/// Gets the endpoint annotation associated with the endpoint reference.
Expand Down Expand Up @@ -71,7 +71,7 @@ public sealed class EndpointReference : IManifestExpressionProvider, IValueProvi
/// The reference will be resolved in the context of this network, which may be different
/// from the network associated with the default network of the referenced Endpoint.
/// </summary>
public NetworkIdentifier ContextNetworkID => _contextNetworkID;
public NetworkIdentifier? ContextNetworkID => _contextNetworkID;

/// <summary>
/// Gets the specified property expression of the endpoint. Defaults to the URL if no property is specified.
Expand Down Expand Up @@ -190,7 +190,7 @@ public EndpointReference(IResourceWithEndpoints owner, EndpointAnnotation endpoi
Resource = owner;
EndpointName = endpoint.Name;
_endpointAnnotation = endpoint;
_contextNetworkID = contextNetworkID ?? KnownNetworkIdentifiers.LocalhostNetwork;
_contextNetworkID = contextNetworkID;
}

/// <summary>
Expand Down Expand Up @@ -221,7 +221,7 @@ public EndpointReference(IResourceWithEndpoints owner, string endpointName, Netw

Resource = owner;
EndpointName = endpointName;
_contextNetworkID = contextNetworkID ?? KnownNetworkIdentifiers.LocalhostNetwork;
_contextNetworkID = contextNetworkID;
}

/// <summary>
Expand Down Expand Up @@ -277,7 +277,7 @@ public class EndpointReferenceExpression(EndpointReference endpointReference, En
/// <exception cref="InvalidOperationException">Throws when the selected <see cref="EndpointProperty"/> enumeration is not known.</exception>
public async ValueTask<string?> GetValueAsync(ValueProviderContext context, CancellationToken cancellationToken = default)
{
var networkContext = context.GetNetworkIdentifier();
var networkContext = Endpoint.ContextNetworkID ?? context.GetNetworkIdentifier();

return Property switch
{
Expand All @@ -292,11 +292,6 @@ public class EndpointReferenceExpression(EndpointReference endpointReference, En
// We are going to take the first snapshot that matches the context network ID. In general there might be multiple endpoints for a single service,
// and in future we might need some sort of policy to choose between them, but for now we just take the first one.
var nes = Endpoint.EndpointAnnotation.AllAllocatedEndpoints.Where(nes => nes.NetworkID == networkContext).FirstOrDefault();
if (nes is null)
{
nes = Endpoint.EndpointAnnotation.AllAllocatedEndpoints.Where(nes => nes.NetworkID == Endpoint.ContextNetworkID).FirstOrDefault();
}

if (nes is null)
{
return null;
Expand Down
1 change: 1 addition & 0 deletions tests/Aspire.Hosting.Tests/Dcp/DcpExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,7 @@ private static DcpExecutor CreateAppExecutor(
{
ServiceProvider = new TestServiceProvider(configuration)
.AddService<IDeveloperCertificateService>(developerCertificateService)
.AddService(Options.Create(dcpOptions))
}),
resourceLoggerService,
new TestDcpDependencyCheckService(),
Expand Down
18 changes: 3 additions & 15 deletions tests/Aspire.Hosting.Tests/ExpressionResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,12 @@ public async Task ExpressionResolverGeneratesCorrectEndpointStrings(string exprN
[InlineData(true, true, "http://127.0.0.1:18889", "http://aspire.dev.internal:18889")]
[InlineData(false, true, "http://[::1]:18889", "http://[::1]:18889")]
[InlineData(true, true, "http://[::1]:18889", "http://aspire.dev.internal:18889")]
[InlineData(false, true, "Server=localhost,1433;User ID=sa;Password=xxx;Database=yyy", "Server=localhost,1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(true, true, "Server=localhost,1433;User ID=sa;Password=xxx;Database=yyy", "Server=aspire.dev.internal,1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(false, true, "Server=127.0.0.1,1433;User ID=sa;Password=xxx;Database=yyy", "Server=127.0.0.1,1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(true, true, "Server=127.0.0.1,1433;User ID=sa;Password=xxx;Database=yyy", "Server=aspire.dev.internal,1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(false, true, "Server=[::1],1433;User ID=sa;Password=xxx;Database=yyy", "Server=[::1],1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(true, true, "Server=[::1],1433;User ID=sa;Password=xxx;Database=yyy", "Server=aspire.dev.internal,1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(false, false, "http://localhost:18889", "http://localhost:18889")]
[InlineData(true, false, "http://localhost:18889", "http://host.docker.internal:18889")]
[InlineData(false, false, "http://127.0.0.1:18889", "http://127.0.0.1:18889")]
[InlineData(true, false, "http://127.0.0.1:18889", "http://host.docker.internal:18889")]
[InlineData(false, false, "http://[::1]:18889", "http://[::1]:18889")]
[InlineData(true, false, "http://[::1]:18889", "http://host.docker.internal:18889")]
[InlineData(false, false, "Server=localhost,1433;User ID=sa;Password=xxx;Database=yyy", "Server=localhost,1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(true, false, "Server=localhost,1433;User ID=sa;Password=xxx;Database=yyy", "Server=host.docker.internal,1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(false, false, "Server=127.0.0.1,1433;User ID=sa;Password=xxx;Database=yyy", "Server=127.0.0.1,1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(true, false, "Server=127.0.0.1,1433;User ID=sa;Password=xxx;Database=yyy", "Server=host.docker.internal,1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(false, false, "Server=[::1],1433;User ID=sa;Password=xxx;Database=yyy", "Server=[::1],1433;User ID=sa;Password=xxx;Database=yyy")]
[InlineData(true, false, "Server=[::1],1433;User ID=sa;Password=xxx;Database=yyy", "Server=host.docker.internal,1433;User ID=sa;Password=xxx;Database=yyy")]
public async Task HostUrlPropertyGetsResolved(bool targetIsContainer, bool withTunnel, string hostUrlVal, string expectedValue)
{
var builder = DistributedApplication.CreateBuilder();
Expand Down Expand Up @@ -270,9 +258,9 @@ sealed class TestValueProviderResource(string name) : Resource(name), IValueProv
sealed class TestExpressionResolverResource : ContainerResource, IResourceWithEndpoints, IResourceWithConnectionString
{
readonly string _exprName;
EndpointReference Endpoint1 => new(this, "endpoint1", KnownNetworkIdentifiers.LocalhostNetwork);
EndpointReference Endpoint2 => new(this, "endpoint2", KnownNetworkIdentifiers.LocalhostNetwork);
EndpointReference Endpoint3 => new(this, "endpoint3", KnownNetworkIdentifiers.LocalhostNetwork);
EndpointReference Endpoint1 => new(this, "endpoint1");
EndpointReference Endpoint2 => new(this, "endpoint2");
EndpointReference Endpoint3 => new(this, "endpoint3");
Dictionary<string, ReferenceExpression> Expressions { get; }
public TestExpressionResolverResource(string exprName) : base("testresource")
{
Expand Down