microsoft/aspire · error · InvalidOperationException

Container app context not found for resource

Error message

Container app context not found for resource {resource.Name}.

What it means

ContainerAppEnvironmentContext keeps a dictionary of per-resource BaseContainerAppContext objects built during publishing. GetContainerAppContext throws this InvalidOperationException when the requested IResource has no registered container app context, meaning the resource was never converted into a Container App (e.g. it is not a container/project resource handled by the environment).

Solutions

  1. Ensure the resource passed to GetContainerAppContext is actually added to the container app environment and is a supported container/project resource
  2. Verify the call happens after the container app contexts have been created (post model build / during publish)
  3. Check the resource name for typos or reference to the wrong resource instance
  4. Use TryGetValue-style access or check the resource type before calling

Example fix

// before
var ctx = envContext.GetContainerAppContext(someExternalResource);
// after
if (someExternalResource is IContainerAppSupportedResource)
{
    var ctx = envContext.GetContainerAppContext(someExternalResource);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (!envContext.KnowsResource(resource)) return; // or check resource kind before lookup

Type guard

func IsPublishedContainerApp(IResource r) => r is ProjectResource or ContainerResource && r.HasAnnotationOfType<ContainerAppModifiedAnnotation>();

Try / catch

try { var ctx = envContext.GetContainerAppContext(resource); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Container app context not found")) { /* resource is not a published container app */ }

Prevention

When it happens

Trigger: Calling GetContainerAppContext for a resource that is not part of the Azure Container App environment's published resources — before CreateContainerAppContext ran for it, or for a resource type (e.g. a plain external resource) that never gets a container app.

Common situations: Custom publishing callbacks/annotations (e.g. AzureContainerAppCustomizationAnnotation callbacks or dashboard URL logic) firing for resources outside the environment; calling the API during model building before publish-time context creation completes.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Azure.AppContainers/ContainerAppEnvironmentContext.cs:66

        _hasLoggedHttpsUpgrade = true;

        var details = string.Join(", ", _upgradedEndpoints.Select(x =>
            x.EndpointNames.Length == 1
                ? $"{x.ResourceName}:{x.EndpointNames[0]}"
                : $"{x.ResourceName}:{{{string.Join(", ", x.EndpointNames)}}}"));

        Logger.LogInformation(
            "HTTP endpoints will use HTTPS (port 443) in Azure Container Apps: {Details}. " +
            "To opt out, use .WithHttpsUpgrade(false) on the container app environment.",
            details);
    }

    public BaseContainerAppContext GetContainerAppContext(IResource resource)
    {
        if (!_containerApps.TryGetValue(resource, out var context))
        {
            throw new InvalidOperationException($"Container app context not found for resource {resource.Name}.");
        }

        return context;
    }

    public async Task<AzureBicepResource> CreateContainerAppAsync(IResource resource, AzureProvisioningOptions provisioningOptions, CancellationToken cancellationToken)
    {
        if (!_containerApps.TryGetValue(resource, out var context))
        {
            _containerApps[resource] = context = CreateContainerAppContext(resource);
            await context.ProcessResourceAsync(cancellationToken).ConfigureAwait(false);
        }

        var provisioningResource = new AzureContainerAppResource(resource.Name + "-containerapp", context.BuildContainerApp, resource)
        {
            ProvisioningBuildOptions = provisioningOptions.ProvisioningBuildOptions
        };

View on GitHub (pinned to 25830f84bd)