microsoft/aspire · error · InvalidOperationException
Resource ' ' is configured to publish as an Azure Container…
Error message
Resource '{r.Name}' is configured to publish as an Azure Container App, but there are no 'AzureContainerAppEnvironmentResource' resources. Ensure you have added one by calling 'AddAzureContainerAppEnvironment'. What it means
During publish orchestration, resources carrying AzureContainerAppCustomizationAnnotation or AzureContainerAppJobCustomizationAnnotation are meant to be published as Azure Container Apps, which requires at least one AzureContainerAppEnvironmentResource in the model. If none exists when the infrastructure is built, this DistributedApplicationException is thrown, naming the offending resource and the missing AddAzureContainerAppEnvironment call.
Solutions
- Add builder.AddAzureContainerAppEnvironment("aca-env") in the AppHost before building/publishing
- Remove Container App customizations (PublishAsAzureContainerApp / related annotations) from resources that shouldn't target Container Apps
- Verify the environment addition is not gated behind a flag or condition that skips it in your publish run
Example fix
// before
var api = builder.AddProject<Projects.Api>("api")
.PublishAsAzureContainerApp(...);
// after
builder.AddAzureContainerAppEnvironment("aca-env");
var api = builder.AddProject<Projects.Api>("api")
.PublishAsAzureContainerApp(...); Defensive patterns
Strategy: validation
Validate before calling
if (!builder.Resources.OfType<AzureContainerAppEnvironmentResource>().Any() &&
builder.Resources.Any(r => r.HasAnnotationOfType<AzureContainerAppCustomizationAnnotation>()))
{
throw new InvalidOperationException("Add AddAzureContainerAppEnvironment before Container App customizations");
} Try / catch
try { await PublishAsync(model); }
catch (DistributedApplicationException ex) when (ex.Message.Contains("AddAzureContainerAppEnvironment"))
{
logger.LogError(ex, "A Container App environment is required");
} Prevention
- Always declare AddAzureContainerAppEnvironment when targeting Container Apps
- Keep environment setup unconditional and early in the AppHost
- Audit annotations/PublishAsAzureContainerApp usage against available environments
When it happens
Trigger: Calling .PublishAsAzureContainerApp()-style APIs or applying Container App customizations/annotations to a compute resource without ever calling builder.AddAzureContainerAppEnvironment(...) before publishing.
Common situations: Porting an AppHost from another deployment target (e.g. ACA via azd manually) to Aspire's Container App publishing and forgetting the environment declaration; conditional environment setup that didn't run; template samples with Container App customizations run through publish.
Understand the failure class
Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.
Related errors
- Azure Container App environments
- Cannot configure custom domain when resource is not…
- Container app context not found for resource
- Container port is required for all endpoints
- No container registry associated with environment
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/c6c3fbbdb76d5512.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.AppContainers/AzureContainerAppExtensions.cs:87
action: ctx =>
{
if (!ctx.ExecutionContext.IsPublishMode)
{
return Task.CompletedTask;
}
var environments = ctx.Model.Resources
.OfType<AzureContainerAppEnvironmentResource>()
.Where(environment => !environment.IsExcludedFromPublish())
.ToList();
if (environments.Count == 0)
{
foreach (var r in ctx.Model.GetComputeResources())
{
if (r.HasAnnotationOfType<AzureContainerAppCustomizationAnnotation>() ||
r.HasAnnotationOfType<AzureContainerAppJobCustomizationAnnotation>())
{
throw new InvalidOperationException($"Resource '{r.Name}' is configured to publish as an Azure Container App, but there are no '{nameof(AzureContainerAppEnvironmentResource)}' resources. Ensure you have added one by calling '{nameof(AddAzureContainerAppEnvironment)}'.");
}
}
}
else
{
// Name resolvers run while each environment's Bicep module is generated. Force evaluation
// here so the shared tracker sees every legacy fallback before deployment targets are prepared.
foreach (var environment in environments)
{
_ = environment.GetBicepTemplateString();
}
marker.ValidateManagedEnvironmentNames(
environments.Select(environment => environment.Name).ToHashSet(StringComparer.Ordinal));
}
return Task.CompletedTask;
},View on GitHub (pinned to 25830f84bd)