microsoft/aspire · error · InvalidOperationException

No container registry associated with environment

Error message

No container registry associated with environment '{appEnvResource.Name}'. This should have been added automatically.

What it means

When building the infrastructure module for a newly added Azure Container App Environment, the code resolves the environment's associated container registry — which the framework is supposed to attach automatically (as DefaultContainerRegistry or via the registry accessor). If no registry is present, this InvalidOperationException is thrown because it indicates the automatic wiring never happened.

Solutions

  1. Align all Aspire.Hosting.Azure.* package versions and rebuild
  2. Remove custom model transformations that remove or alter the Container App environment's registry annotations
  3. Ensure the environment is created via builder.AddAzureContainerAppEnvironment (not manually constructed) so auto-wiring runs
  4. If it persists, file an Aspire issue with your AppHost and publish output
Defensive patterns

Strategy: validation

Validate before calling

if (appEnvResource.DefaultContainerRegistry is null)
{
    throw new InvalidOperationException($"Environment '{appEnvResource.Name}' lacks its auto-added registry; check package versions");
}

Try / catch

try { await PublishAsync(model); }
catch (InvalidOperationException ex) when (ex.Message.Contains("No container registry associated"))
{
    logger.LogError(ex, "Automatic registry wiring missing for Container App environment");
}

Prevention

When it happens

Trigger: Publishing a model where an AzureContainerAppEnvironmentResource reaches registry wiring (AddAzureContainerAppEnvironment publish path) with both the environment-provided registry and DefaultContainerRegistry null — i.e. the framework's automatic registry initialization was skipped or failed.

Common situations: Version skew between Aspire.Hosting.Azure.AppContainers packages, custom model transformations that strip environment annotations/resources before publish, or partial initialization when constructing the environment resource programmatically in tests.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/479924c053b7fb8b. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.AppContainers/AzureContainerAppExtensions.cs:349

                infra.Add(newIdentity);
                managedIdentityIdOutputValue = newIdentity.Id.ToBicepExpression();
            }

            AzureProvisioningResource? registry = null;
            if (appEnvResource.TryGetLastAnnotation<ContainerRegistryReferenceAnnotation>(out var registryReferenceAnnotation) &&
                registryReferenceAnnotation.Registry is AzureProvisioningResource explicitRegistry)
            {
                registry = explicitRegistry;
            }
            else if (appEnvResource.DefaultContainerRegistry is not null)
            {
                registry = appEnvResource.DefaultContainerRegistry;
            }

            if (registry is null)
            {
                throw new InvalidOperationException($"No container registry associated with environment '{appEnvResource.Name}'. This should have been added automatically.");
            }

            var containerRegistry = (ContainerRegistryService)registry.AddAsExistingResource(infra);
            infra.Add(containerRegistry);

            if (newIdentity is not null)
            {
                var pullRa = containerRegistry.CreateRoleAssignment(ContainerRegistryBuiltInRole.AcrPull, newIdentity);

                // There's a bug in the CDK, see https://github.com/Azure/azure-sdk-for-net/issues/47265
                pullRa.Name = BicepFunction.CreateGuid(containerRegistry.Id, newIdentity.Id, pullRa.RoleDefinitionId);
                infra.Add(pullRa);
            }

            OperationalInsightsWorkspace? laWorkspace = null;
            if (appEnvResource.TryGetLastAnnotation<AzureLogAnalyticsWorkspaceReferenceAnnotation>(out var logAnalyticsReferenceAnnotation) && logAnalyticsReferenceAnnotation.Workspace is AzureProvisioningResource workspace)
            {
                laWorkspace = (OperationalInsightsWorkspace)workspace.AddAsExistingResource(infra);

View on GitHub (pinned to 25830f84bd)