Custom allocated endpoint no longer working
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
I have a test that used to pass on 13.2, but fails now on 13.3.
My test needs to spin up a small web server on the test, which then needs to be called from within a container. This needs to be in memory in my test host as part of my test's assertions are to assert that the endpoint was called correctly.
The old test only ever passed when the container tunnel was off. I know the container tunnel has being turned on by default in 13.3, but it looks like I should still be able to disable it if I want. But even after disabling the tunnel, my test still fails, and I also see the tunnel being provisioned in Docker so it as
1. Do you still expect to be able to opt out of the container tunnel in 13.3? If so, why is the below code still trying to use the container tunnel
2. How do I adapt this scenario to work with the tunnel
### Expected Behavior
All 4 scenarios should get back the "Hello Metrics" response
Actual behaiour:
✅ 13.2.4 (`useContainerTunnel = false`)
> Hello, Metrics!
❌ 13.2.4 (`useContainerTunnel = true`)
> curl: (7) Failed to connect to aspire.dev.internal port 56046 after 2 ms: Could not connect to server
❌ 13.3.0 (`useContainerTunnel = false`)
> curl: (7) Failed to connect to host.docker.internal port 61553 after 0 ms: Could not connect to server
❌ 13.3.0 (`useContainerTunnel = true`)
> curl: (7) Failed to connect to aspire.dev.internal port 61944 after 1 ms: Could not connect to server
### Steps To Reproduce
This app host creates two resources
1. A kestrel in memory web server
2. A container resource which `curl`s an endpoint from the first resource
```cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Net;
// Seems to fail both with and without the container tunnel
bool useContainerTunnel = false;
var builder = DistributedApplication.CreateBuilder(args);
builder.Configuration["ASPIRE_ENABLE_CONTAINER_TUNNEL"] = useContainerTunnel.ToString();
var server = AddWebServerResource(builder);
var container = builder.AddContainer("curl", "mcr.microsoft.com/azurelinux/base/core:3.0")
.WithEntrypoint("sh")
.WithArgs("-c", "curl $URL")
.WithEnvironment("URL", server.GetEndpoint("http"))
.WaitFor(server);
builder.Build().Run();
IResourceBuilder AddWebServerResource(IDistributedApplicationBuilder builder)
{
var resource = new MetricsResource();
var server = builder.AddResource(resource)
.WithHttpEndpoint();
var endpoint = server.GetEndpoint("metrics");
return server.OnInitializeResource(async (resource, evt, ct) =>
{
try
{
var webBuilder = WebApplication.CreateSlimBuilder();
webBuilder.WebHost.ConfigureKestrel(x => x.Listen(IPAddress.Loopback, 0));
var app = webBuilder.Build();
app.MapGet("/", () => "Hello, Metrics!");
await app.StartAsync(ct);
var addressFeature = app.Services.GetRequiredService()
.Features
.Get();
var address = BindingAddress.Parse(addressFeature!.Addresses.Single());
var containerHost = useContainerTunnel ? KnownHostNames.DefaultContainerTunnelHostName : KnownHostNames.DockerDesktopHostBridge;
var endpointAnnotation = resource.Annotations.OfType().Single();
var hostEndpoint = new AllocatedEndpoint(endpointAnnotation, address.Host, address.Port, EndpointBindingMode.SingleAddress, targetPortExpression: address.Port.ToString(), networkID: KnownNetworkIdentifiers.LocalhostNetwork);
var containerEndpoint = new AllocatedEndpoint(endpointAnnotation, containerHost, address.Port, EndpointBindingMode.SingleAddress, targetPortExpression: address.Port.ToString(), networkID: KnownNetworkIdentifiers.DefaultAspireContainerNetwork);
endpointAnnotation.AllAllocatedEndpoints.AddOrUpdateAllocatedEndpoint(KnownNetworkIdentifiers.LocalhostNetwork, hostEndpoint);
endpointAnnotation.AllAllocatedEndpoints.AddOrUpdateAllocatedEndpoint(KnownNetworkIdentifiers.DefaultAspireContainerNetwork, containerEndpoint);
await evt.Eventing.PublishAsync(new ResourceEndpointsAllocatedEvent(resource, evt.Services), ct);
ct.Register(() => app.StopAsync().Wait());
await evt.Eventing.PublishAsync(new BeforeResourceStartedEvent(resource, evt.Services), ct);
await evt.Notifications.PublishUpdateAsync(resource, s => s with
{
StartTimeStamp = DateTime.UtcNow,
State = KnownResourceStates.Running // Use an Aspire well-known state.
});
}
catch (Exception ex)
{
evt.Logger.LogError(ex, "Failed to initialise resource");
}
});
}
class MetricsResource() :
Resource(nameof(MetricsResource)), IResourceWithEndpoints
{
}
```
### Exceptions (if any)
_No response_
### Aspire doctor output
Aspire Environment Check
========================
.NET SDK
✅ .NET 10.0.203 installed (x64)
Container Runtime
✅ Docker v29.4.1: running (auto-detected (default)) ← active
Environment
✅ HTTPS development certificate is trusted
### Anything else?
13.3.0+be8c19e483c7fca17f64510d1713ce5228a58843
Contributor guide
Assessment
This issue has not been assessed yet.