{"record":{"id":"d1c4e8b16a4157c1","repo":"microsoft/aspire","slug":"container-app-context-not-found-for-resource-resource-name","errorCode":null,"errorMessage":"Container app context not found for resource {resource.Name}.","messagePattern":"Container app context not found for resource (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.AppContainers/ContainerAppEnvironmentContext.cs","lineNumber":66,"sourceCode":"\n        _hasLoggedHttpsUpgrade = true;\n\n        var details = string.Join(\", \", _upgradedEndpoints.Select(x =>\n            x.EndpointNames.Length == 1\n                ? $\"{x.ResourceName}:{x.EndpointNames[0]}\"\n                : $\"{x.ResourceName}:{{{string.Join(\", \", x.EndpointNames)}}}\"));\n\n        Logger.LogInformation(\n            \"HTTP endpoints will use HTTPS (port 443) in Azure Container Apps: {Details}. \" +\n            \"To opt out, use .WithHttpsUpgrade(false) on the container app environment.\",\n            details);\n    }\n\n    public BaseContainerAppContext GetContainerAppContext(IResource resource)\n    {\n        if (!_containerApps.TryGetValue(resource, out var context))\n        {\n            throw new InvalidOperationException($\"Container app context not found for resource {resource.Name}.\");\n        }\n\n        return context;\n    }\n\n    public async Task<AzureBicepResource> CreateContainerAppAsync(IResource resource, AzureProvisioningOptions provisioningOptions, CancellationToken cancellationToken)\n    {\n        if (!_containerApps.TryGetValue(resource, out var context))\n        {\n            _containerApps[resource] = context = CreateContainerAppContext(resource);\n            await context.ProcessResourceAsync(cancellationToken).ConfigureAwait(false);\n        }\n\n        var provisioningResource = new AzureContainerAppResource(resource.Name + \"-containerapp\", context.BuildContainerApp, resource)\n        {\n            ProvisioningBuildOptions = provisioningOptions.ProvisioningBuildOptions\n        };\n","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.AppContainers/ContainerAppEnvironmentContext.cs#L48-L84","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the resource passed to GetContainerAppContext is actually added to the container app environment and is a supported container/project resource","Verify the call happens after the container app contexts have been created (post model build / during publish)","Check the resource name for typos or reference to the wrong resource instance","Use TryGetValue-style access or check the resource type before calling"],"exampleFix":"// before\nvar ctx = envContext.GetContainerAppContext(someExternalResource);\n// after\nif (someExternalResource is IContainerAppSupportedResource)\n{\n    var ctx = envContext.GetContainerAppContext(someExternalResource);\n}","handlingStrategy":"type-guard","validationCode":"if (!envContext.KnowsResource(resource)) return; // or check resource kind before lookup","typeGuard":"func IsPublishedContainerApp(IResource r) => r is ProjectResource or ContainerResource && r.HasAnnotationOfType<ContainerAppModifiedAnnotation>();","tryCatchPattern":"try { var ctx = envContext.GetContainerAppContext(resource); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Container app context not found\")) { /* resource is not a published container app */ }","preventionTips":["Only call GetContainerAppContext for resources added to the container app environment","Call after container app contexts are created (publish time), not during model building","Check resource type/naming before lookup"],"tags":["azure","container-apps","publish","lookup"],"backgroundTag":"resource-not-found","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}