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

  1. Add builder.AddAzureContainerAppEnvironment("aca-env") in the AppHost before building/publishing
  2. Remove Container App customizations (PublishAsAzureContainerApp / related annotations) from resources that shouldn't target Container Apps
  3. 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

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


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)