microsoft/aspire · error · InvalidOperationException

Container app environment id

Error message

Container app environment id '{containerAppEnvironmentId}' does not contain a subscription id.

What it means

GetSubscriptionAndResourceGroup parses the environment ID with Azure ResourceIdentifier and needs a subscription component to construct a portal resource-group URL. If the parsed ID has no subscription id, this InvalidOperationException is thrown.

Solutions

  1. Ensure the environment ID is a full ARM ID: /subscriptions/<sub>/resourceGroups/<rg>/providers/Microsoft.App/managedEnvironments/<name>
  2. Check the Bicep/module output for the managed environment uses its .id property
  3. Validate the output value format before parsing with ResourceIdentifier
  4. Fix upstream deployments that return truncated IDs

Example fix

// before
envOutput = "/providers/Microsoft.App/managedEnvironments/env1"; // no subscription
// after
envOutput = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.App/managedEnvironments/env1";
Defensive patterns

Strategy: validation

Validate before calling

if (Azure.ResourceManager.ResourceIdentifier.TryParse(environmentId, out var rid) && rid.SubscriptionId is not null)
{
    // safe to parse
}

Type guard

bool HasSubscription(string? id) => id is not null && id.Contains("/subscriptions/");

Try / catch

try { var link = await ContainerAppUrls.GetPortalLinkAsync(env, name, ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("does not contain a subscription id")) { /* log malformed environment id */ }

Prevention

When it happens

Trigger: ContainerAppEnvironmentId output value is not a fully-qualified ARM resource ID (e.g. a name, relative ID like /providers/..., or malformed string without /subscriptions/<id>/).

Common situations: Custom or stubbed environment ID values in tests/local emulation; hand-edited Bicep outputs; downstream tooling replacing the output with a non-ARM identifier.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Azure.AppContainers/ContainerAppUrls.cs:28

internal static class ContainerAppUrls
{
    private const string ContainerAppResourceType = "/providers/Microsoft.App/containerApps/";

    internal static async Task<MarkdownString> GetPortalLinkAsync(AzureContainerAppEnvironmentResource containerAppEnv, string containerAppName, CancellationToken cancellationToken)
    {
        var environmentIdValue = await containerAppEnv.ContainerAppEnvironmentId.GetValueAsync(cancellationToken).ConfigureAwait(false)
            ?? throw new InvalidOperationException($"Missing container app environment id output for '{containerAppEnv.Name}'.");
        var (subscriptionId, resourceGroupName) = GetSubscriptionAndResourceGroup(environmentIdValue);
        var resourceId = $"{AzurePortalUrls.GetResourceGroupResourceId(subscriptionId, resourceGroupName)}{ContainerAppResourceType}{containerAppName}";

        return AzurePortalUrls.GetResourceLink(resourceId);
    }

    private static (string SubscriptionId, string ResourceGroupName) GetSubscriptionAndResourceGroup(string containerAppEnvironmentId)
    {
        var environmentId = new ResourceIdentifier(containerAppEnvironmentId);
        var subscriptionId = environmentId.SubscriptionId ?? throw new InvalidOperationException($"Container app environment id '{containerAppEnvironmentId}' does not contain a subscription id.");
        var resourceGroupName = environmentId.ResourceGroupName ?? throw new InvalidOperationException($"Container app environment id '{containerAppEnvironmentId}' does not contain a resource group name.");

        return (subscriptionId, resourceGroupName);
    }
}

View on GitHub (pinned to 25830f84bd)