microsoft/aspire · error · InvalidOperationException
App Service context not found for resource
Error message
App Service context not found for resource {resource.Name}. What it means
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.
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.
Example fix
// before
var ctx = envContext.GetAppServiceContext(anyResource);
// after
if (anyResource.HasAnnotationOfType<AzureAppServiceWebsiteCustomizationAnnotation>())
{
var ctx = envContext.GetAppServiceContext(anyResource);
} Defensive patterns
Strategy: validation
Validate before calling
if (!resource.HasAnnotationOfType<AzureAppServiceWebsiteCustomizationAnnotation>()) throw new InvalidOperationException("Resource is not an App Service website; no context will exist."); Type guard
bool HasAppServiceContext(IResource r) => r.HasAnnotationOfType<AzureAppServiceWebsiteCustomizationAnnotation>();
Try / catch
try { var ctx = envContext.GetAppServiceContext(resource); } catch (InvalidOperationException ex) { logger.LogWarning(ex, "No App Service context for {Resource}", resource.Name); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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).
Understand the failure class
Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.
Related errors
- App Service configuration validation failed. See errors…
- App Service plan id ' ' does not contain a resource group…
- Application Insights must be omitted, a location string, a…
- configure or configureSlot must be provided.
- Deployment slot must be a string or a parameter resource…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/f48008344f4ea1aa.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.AppService/AzureAppServiceEnvironmentContext.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 App Service: {Details}. " +
"To opt out, use .WithHttpsUpgrade(false) on the app service environment.",
details);
}
public AzureAppServiceWebsiteContext GetAppServiceContext(IResource resource)
{
if (!_appServices.TryGetValue(resource, out var context))
{
throw new InvalidOperationException($"App Service context not found for resource {resource.Name}.");
}
return context;
}
public async Task<AzureBicepResource> CreateAppServiceAsync(IResource resource, AzureProvisioningOptions provisioningOptions, CancellationToken cancellationToken)
{
if (!_appServices.TryGetValue(resource, out var context))
{
_appServices[resource] = context = new AzureAppServiceWebsiteContext(resource, this);
await context.ProcessAsync(cancellationToken).ConfigureAwait(false);
}
var provisioningResource = new AzureAppServiceWebSiteResource(resource.Name + "-website", context.BuildWebSite, resource)
{
ProvisioningBuildOptions = provisioningOptions.ProvisioningBuildOptions
};
View on GitHub (pinned to 25830f84bd)