microsoft/aspire · error · InvalidOperationException

The resource ' ' does not have an associated Azure…

Error message

The resource '{builder.Resource.Name}' does not have an associated Azure Container Registry.

What it means

GetAzureContainerRegistry retrieves the Azure Container Registry associated with a compute environment resource (e.g. an Azure Container App environment). It throws InvalidOperationException when the resource's ContainerRegistry property is null, meaning no registry has been associated with that compute environment.

Solutions

  1. Associate a container registry with the compute environment before calling the getter (e.g. AddAzureContainerRegistry on the environment)
  2. Call GetAzureContainerRegistry only on environment resources known to have a registry (check Resource.ContainerRegistry first)
  3. Verify you are calling it in run/publish phase after model enrichment has run

Example fix

// before
var registry = builder.GetAzureContainerRegistry();
// after
var env = builder.AddAzureContainerAppEnvironment("env").WithAzureContainerRegistry();
var registry = env.GetAzureContainerRegistry();
Defensive patterns

Strategy: type-guard

Validate before calling

if (env.Resource.ContainerRegistry is null)
    throw new InvalidOperationException("Associate a registry with the environment before calling GetAzureContainerRegistry.");

Type guard

var registry = env.Resource.ContainerRegistry as AzureContainerRegistryResource;
if (registry is null) { /* handle missing or wrong-type registry */ }

Try / catch

try { var registry = env.GetAzureContainerRegistry(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("does not have an associated Azure Container Registry")) { log.LogError(ex, "No registry on {Env}", env.Resource.Name); }

Prevention

When it happens

Trigger: Calling GetAzureContainerRegistry on a compute environment resource that was created without an accompanying container registry, or before registry association was configured.

Common situations: Adding a container apps environment via AddAzureContainerAppEnvironment without the registry wiring; calling the getter in app-model code before the environment has run its provisioning/association callbacks.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs:118

    /// <summary>
    /// Gets the <see cref="AzureContainerRegistryResource"/> associated with the specified Azure compute environment resource.
    /// </summary>
    /// <ats-summary>Gets the Azure Container Registry associated with a compute environment resource.</ats-summary>
    /// <typeparam name="T">The resource type that implements <see cref="IAzureComputeEnvironmentResource"/>.</typeparam>
    /// <param name="builder">The resource builder for the compute environment resource.</param>
    /// <returns>A reference to the <see cref="IResourceBuilder{AzureContainerRegistryResource}"/> for the associated registry.</returns>
    /// <ats-returns>The resource builder.</ats-returns>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> is <see langword="null"/>.</exception>
    /// <exception cref="InvalidOperationException">Thrown when the resource does not have an associated Azure Container Registry,
    /// or when the associated container registry is not an <see cref="AzureContainerRegistryResource"/>.</exception>
    [AspireExport]
    public static IResourceBuilder<AzureContainerRegistryResource> GetAzureContainerRegistry<T>(this IResourceBuilder<T> builder)
        where T : IResource, IAzureComputeEnvironmentResource
    {
        ArgumentNullException.ThrowIfNull(builder);

        var containerRegistry = builder.Resource.ContainerRegistry ?? throw new InvalidOperationException($"The resource '{builder.Resource.Name}' does not have an associated Azure Container Registry.");
        var registry = containerRegistry as AzureContainerRegistryResource ?? throw new InvalidOperationException($"The Container Registry associated with resource '{builder.Resource.Name}' is not an Azure Container Registry.");

        return builder.ApplicationBuilder.CreateResourceBuilder(registry);
    }

    /// <summary>
    /// Adds a scheduled ACR purge task to remove old or unused container images from the registry.
    /// </summary>
    /// <param name="builder">The resource builder for the <see cref="AzureContainerRegistryResource"/>.</param>
    /// <param name="schedule">The cron schedule for the purge task timer trigger. Must be a five-part cron expression
    /// (<c>minute hour day-of-month month day-of-week</c>); seconds are not supported.</param>
    /// <param name="filter">An optional filter for the <c>acr purge --filter</c> parameter. Only repositories matching this
    /// filter will be purged. Defaults to <c>".*:.*"</c> (all repositories and tags) when <see langword="null"/>.</param>
    /// <param name="ago">The age threshold for <c>acr purge --ago</c>. Images older than this duration will be considered
    /// for removal. Uses Go-style duration format (e.g., <c>2d3h6m</c>). Defaults to <c>0d</c> when <see langword="null"/>.</param>
    /// <param name="keep">The number of most recent images to keep per repository, regardless of age.
    /// Must be greater than zero. Defaults to 3.</param>
    /// <param name="taskName">An optional name for the ACR task resource. If not provided, a name is auto-generated

View on GitHub (pinned to 25830f84bd)