{"record":{"id":"f48008344f4ea1aa","repo":"microsoft/aspire","slug":"app-service-context-not-found-for-resource-resource-name","errorCode":null,"errorMessage":"App Service context not found for resource {resource.Name}.","messagePattern":"App Service context not found for resource (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.AppService/AzureAppServiceEnvironmentContext.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 App Service: {Details}. \" +\n            \"To opt out, use .WithHttpsUpgrade(false) on the app service environment.\",\n            details);\n    }\n\n    public AzureAppServiceWebsiteContext GetAppServiceContext(IResource resource)\n    {\n        if (!_appServices.TryGetValue(resource, out var context))\n        {\n            throw new InvalidOperationException($\"App Service context not found for resource {resource.Name}.\");\n        }\n\n        return context;\n    }\n\n    public async Task<AzureBicepResource> CreateAppServiceAsync(IResource resource, AzureProvisioningOptions provisioningOptions, CancellationToken cancellationToken)\n    {\n        if (!_appServices.TryGetValue(resource, out var context))\n        {\n            _appServices[resource] = context = new AzureAppServiceWebsiteContext(resource, this);\n            await context.ProcessAsync(cancellationToken).ConfigureAwait(false);\n        }\n\n        var provisioningResource = new AzureAppServiceWebSiteResource(resource.Name + \"-website\", context.BuildWebSite, resource)\n        {\n            ProvisioningBuildOptions = provisioningOptions.ProvisioningBuildOptions\n        };\n","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.AppService/AzureAppServiceEnvironmentContext.cs#L48-L84","documentation":"AzureAppServiceEnvironmentContext keeps a per-resource map of AzureAppServiceWebsiteContext instances created during App Service infrastructure generation. GetAppServiceContext throws this error when asked for a resource that never had a context created — meaning the resource is not registered as an App Service website in this environment.","triggerScenarios":"Calling GetAppServiceContext for an IResource that was never processed by CreateAppServiceAsync / the App Service compute callbacks — e.g. a resource without the App Service customization annotation, a plain container without PublishAsAzureAppServiceWebsite, or a resource added to the model after contexts were built.","commonSituations":"Custom publishing pipeline steps that query contexts for arbitrary model resources; resources added via polyglot/other providers that bypass App Service registration; referencing a different resource instance than the one registered (dictionary is keyed by IResource identity).","solutions":["Only call GetAppServiceContext for resources configured with PublishAsAzureAppServiceWebsite.","Verify the exact IResource instance passed matches the one registered (not a copy or different builder's resource).","Create the context via the environment context's creation path (CreateAppServiceAsync / context map population) before fetching it.","Check for the AzureAppServiceWebsiteCustomizationAnnotation before calling."],"exampleFix":"// before\nvar ctx = envContext.GetAppServiceContext(anyResource);\n// after\nif (anyResource.HasAnnotationOfType<AzureAppServiceWebsiteCustomizationAnnotation>())\n{\n    var ctx = envContext.GetAppServiceContext(anyResource);\n}","handlingStrategy":"validation","validationCode":"if (!resource.HasAnnotationOfType<AzureAppServiceWebsiteCustomizationAnnotation>()) throw new InvalidOperationException(\"Resource is not an App Service website; no context will exist.\");","typeGuard":"bool HasAppServiceContext(IResource r) => r.HasAnnotationOfType<AzureAppServiceWebsiteCustomizationAnnotation>();","tryCatchPattern":"try { var ctx = envContext.GetAppServiceContext(resource); } catch (InvalidOperationException ex) { logger.LogWarning(ex, \"No App Service context for {Resource}\", resource.Name); }","preventionTips":["Only fetch contexts for resources processed by the App Service pipeline.","Pass the same IResource instance that was registered, not a clone.","Register contexts before any consumer queries them in custom pipeline steps."],"tags":["azure","appservice","internal-state"],"backgroundTag":"record-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"}