microsoft/aspire · error · InvalidOperationException

Endpoint ' ' on resource ' ' does not have a target port…

Error message

Endpoint '{resolvedEndpoint.Endpoint.Name}' on resource '{resource.TargetResource.Name}' does not have a target port. Configure a target port before deploying it to an Azure sandbox.

What it means

Before wiring a sandbox ingress port, the deployment resolves the endpoint's target port. If ResolveSandboxTargetPort yields null (the endpoint has no target port configured), an InvalidOperationException is thrown because the sandbox must know the concrete container/project port to forward to.

Solutions

  1. Configure a fixed target port on the endpoint via WithTargetPort(int) or the targetPort parameter
  2. Ensure the project/container listens on that fixed port
  3. Check the endpoint annotation's TargetPort before deploying

Example fix

// before
.WithEndpoint(scheme: "http", port: 8080)
// after
.WithEndpoint(scheme: "http", port: 8080, targetPort: 8080)
Defensive patterns

Strategy: validation

Validate before calling

if (endpoint.TargetPort is null)
    throw new InvalidOperationException("Endpoint must have a fixed TargetPort before sandbox deployment.");

Type guard

bool HasFixedTargetPort(EndpointAnnotation e) => e.TargetPort is int;

Try / catch

try { DeploySandbox(...); } catch (InvalidOperationException ex) when (ex.Message.Contains("does not have a target port")) { /* set targetPort on endpoint */ }

Prevention

When it happens

Trigger: An endpoint is created without WithTargetPort/TargetPort (i.e. a dynamically assigned target port) on a resource being deployed to an Azure sandbox.

Common situations: Using WithEndpoint without targetPort and relying on runtime port assignment; endpoints added for local dev only then deployed to sandbox.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/48cbd9c5fa26ee32. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.Sandboxes/AzureSandboxContainerDeployment.cs:1506

                continue;
            }

            if (resource.TargetResource is ProjectResource &&
                string.Equals(resolvedEndpoint.Endpoint.UriScheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) &&
                !resolvedEndpoints.Any(candidate =>
                    string.Equals(candidate.Endpoint.UriScheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) &&
                    candidate.TargetPort.Value == resolvedEndpoint.TargetPort.Value))
            {
                throw new NotSupportedException(
                    $"Endpoint '{resolvedEndpoint.Endpoint.Name}' on project resource '{resource.TargetResource.Name}' is exposed through Azure sandbox ingress, which terminates TLS and forwards plaintext HTTP. " +
                    "Add an HTTP endpoint that shares this endpoint's target port.");
            }

            var targetPort = ResolveSandboxTargetPort(resource.TargetResource, resolvedEndpoint);

            if (targetPort is not int resolvedTargetPort)
            {
                throw new InvalidOperationException($"Endpoint '{resolvedEndpoint.Endpoint.Name}' on resource '{resource.TargetResource.Name}' does not have a target port. Configure a target port before deploying it to an Azure sandbox.");
            }

            var protocol = ResolveSandboxPortProtocol(resource.TargetResource, resolvedEndpoint.Endpoint);
            AzureSandboxEndpointOptions? resolvedEndpointOptions = null;
            endpointOptions?.TryGetValue(resolvedEndpoint.Endpoint.Name, out resolvedEndpointOptions);
            unmatchedEndpointOptions?.Remove(resolvedEndpoint.Endpoint.Name);
            var endpoint = new SandboxEndpoint(
                resolvedEndpoint.Endpoint.Name,
                resolvedTargetPort,
                IsExternal: true,
                IsHttp: true,
                protocol,
                resolvedEndpointOptions?.Anonymous);

            if (endpoints.TryGetValue(resolvedTargetPort, out var existingEndpoint))
            {
                if (!string.Equals(existingEndpoint.Protocol, endpoint.Protocol, StringComparison.Ordinal))
                {

View on GitHub (pinned to 25830f84bd)