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
test
  • Loading branch information
BrennanConroy committed Jul 11, 2025
commit 039f225e14dd878b65a8d07dd33a18b6f533fa0d
70 changes: 35 additions & 35 deletions src/Aspire.Hosting/Orchestrator/ApplicationOrchestrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,54 +229,54 @@ private async Task ProcessResourceUrlCallbacks(IResource resource, CancellationT
}
}
}
}

if (resource.TryGetUrls(out var existingUrls))
{
// Static URLs added to the resource via WithUrl(string name, string url), i.e. not callback-based
urls.AddRange(existingUrls);
}
if (resource.TryGetUrls(out var existingUrls))
{
// Static URLs added to the resource via WithUrl(string name, string url), i.e. not callback-based
urls.AddRange(existingUrls);
}

// Run the URL callbacks
if (resource.TryGetAnnotationsOfType<ResourceUrlsCallbackAnnotation>(out var callbacks))
// Run the URL callbacks
if (resource.TryGetAnnotationsOfType<ResourceUrlsCallbackAnnotation>(out var callbacks))
{
var urlsCallbackContext = new ResourceUrlsCallbackContext(_executionContext, resource, urls, cancellationToken)
{
var urlsCallbackContext = new ResourceUrlsCallbackContext(_executionContext, resource, urls, cancellationToken)
{
Logger = _loggerService.GetLogger(resource.Name)
};
foreach (var callback in callbacks)
{
await callback.Callback(urlsCallbackContext).ConfigureAwait(false);
}
Logger = _loggerService.GetLogger(resource.Name)
};
foreach (var callback in callbacks)
{
await callback.Callback(urlsCallbackContext).ConfigureAwait(false);
}
}

// Clear existing URLs
if (existingUrls is not null)
// Clear existing URLs
if (existingUrls is not null)
{
var existing = existingUrls.ToArray();
for (var i = existing.Length - 1; i >= 0; i--)
{
var existing = existingUrls.ToArray();
for (var i = existing.Length - 1; i >= 0; i--)
{
var url = existing[i];
resource.Annotations.Remove(url);
}
var url = existing[i];
resource.Annotations.Remove(url);
}
}

// Convert relative endpoint URLs to absolute URLs
foreach (var url in urls)
// Convert relative endpoint URLs to absolute URLs
foreach (var url in urls)
{
if (url.Endpoint is { } endpoint)
{
if (url.Endpoint is { } endpoint)
if (url.Url.StartsWith('/') && endpoint.AllocatedEndpoint is { } allocatedEndpoint)
{
if (url.Url.StartsWith('/') && endpoint.AllocatedEndpoint is { } allocatedEndpoint)
{
url.Url = allocatedEndpoint.UriString.TrimEnd('/') + url.Url;
}
url.Url = allocatedEndpoint.UriString.TrimEnd('/') + url.Url;
}
}
}

// Add URLs
foreach (var url in urls)
{
resource.Annotations.Add(url);
}
// Add URLs
foreach (var url in urls)
{
resource.Annotations.Add(url);
}
}

Expand Down
66 changes: 66 additions & 0 deletions tests/Aspire.Hosting.Tests/WithEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,72 @@ public void WithEndpoint_WithAllArguments_ForwardsAllArguments()
Assert.Equal(System.Net.Sockets.ProtocolType.Tcp, endpoint.Protocol);
}

[Fact]
public async Task LocalhostTopLevelDomainSetsAnnotationValues()
{
using var builder = TestDistributedApplicationBuilder.Create();

var tcs = new TaskCompletionSource();
var projectA = builder.AddProject<ProjectA>("projecta")
.WithHttpsEndpoint()
.WithEndpoint("https", e => e.TargetHost = "example.localhost", createIfNotExists: false)
.OnBeforeResourceStarted((_, _, _) =>
{
tcs.SetResult();
return Task.CompletedTask;
});

var app = await builder.BuildAsync();
await app.StartAsync();
await tcs.Task;

var urls = projectA.Resource.Annotations.OfType<ResourceUrlAnnotation>();
Assert.Collection(urls,
url => Assert.StartsWith("https://localhost:", url.Url),
url => Assert.StartsWith("https://example.localhost:", url.Url));

EndpointAnnotation endpoint = Assert.Single(projectA.Resource.Annotations.OfType<EndpointAnnotation>());
Assert.NotNull(endpoint.AllocatedEndpoint);
Assert.Equal(EndpointBindingMode.SingleAddress, endpoint.AllocatedEndpoint.BindingMode);
Assert.Equal("localhost", endpoint.AllocatedEndpoint.Address);

await app.StopAsync();
}

[Theory]
[InlineData("0.0.0.0", EndpointBindingMode.IPv4AnyAddresses)]
//[InlineData("::", EndpointBindingMode.IPv6AnyAddresses)] // Need to figure out a good way to check that Ipv6 binding is supported
public async Task TopLevelDomainSetsAnnotationValues(string host, EndpointBindingMode endpointBindingMode)
{
using var builder = TestDistributedApplicationBuilder.Create();

var tcs = new TaskCompletionSource();
var projectA = builder.AddProject<ProjectA>("projecta")
.WithHttpsEndpoint()
.WithEndpoint("https", e => e.TargetHost = host, createIfNotExists: false)
.OnBeforeResourceStarted((_, _, _) =>
{
tcs.SetResult();
return Task.CompletedTask;
});

var app = await builder.BuildAsync();
await app.StartAsync();
await tcs.Task;

var urls = projectA.Resource.Annotations.OfType<ResourceUrlAnnotation>();
Assert.Collection(urls,
url => Assert.StartsWith("https://localhost:", url.Url),
url => Assert.StartsWith($"https://{Environment.MachineName}:", url.Url));

EndpointAnnotation endpoint = Assert.Single(projectA.Resource.Annotations.OfType<EndpointAnnotation>());
Assert.NotNull(endpoint.AllocatedEndpoint);
Assert.Equal(endpointBindingMode, endpoint.AllocatedEndpoint.BindingMode);
Assert.Equal("localhost", endpoint.AllocatedEndpoint.Address);

await app.StopAsync();
}

private sealed class TestProject : IProjectMetadata
{
public string ProjectPath => "projectpath";
Expand Down
Loading