microsoft/aspire · error · InvalidOperationException

The container registry configured for the Azure Cognitive…

Error message

The container registry configured for the Azure Cognitive Services project '{Name}' is not an Azure Container Registry. Only Azure Container Registry resources are supported. Use '.WithAzureContainerRegistry()' to configure an Azure Container Registry.

What it means

A Foundry project's container registry must be an AzureContainerRegistryResource. When the configured registry is some other resource type, the internal registry property getter throws this InvalidOperationException and points at WithAzureContainerRegistry() as the correct configuration API.

Solutions

  1. Use .WithAzureContainerRegistry(azureContainerRegistryBuilder) with an AddAzureContainerRegistry-created resource
  2. Remove or replace the non-ACR registry assignment on the project
  3. Verify the resource type is AzureContainerRegistryResource before assigning it

Example fix

// before
project.WithContainerRegistry(myGenericRegistry);

// after
var acr = builder.AddAzureContainerRegistry("acr");
project.WithAzureContainerRegistry(acr);
Defensive patterns

Strategy: type-guard

Validate before calling

if (registry is not AzureContainerRegistryResource)
{
    throw new InvalidOperationException("Only Azure Container Registry resources are supported.");
}
project.WithAzureContainerRegistry(registry);

Type guard

static bool IsAzureContainerRegistry(IResource r) => r is AzureContainerRegistryResource;

Try / catch

try { /* access project registry / publish */ }
catch (InvalidOperationException ex) when (ex.Message.Contains("not an Azure Container Registry"))
{
    // Reconfigure with AddAzureContainerRegistry.
}

Prevention

When it happens

Trigger: Assigning a generic container registry / non-ACR registry resource to the project's registry property, then accessing the typed AzureContainerRegistryResource property (e.g. during infrastructure generation).

Common situations: Using a Docker/other registry resource or a plain ConnectionString resource where an ACR resource is expected; older code configured before the ACR-only requirement; passing the wrong builder to the registry-assigning API.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Foundry/Project/ProjectResource.cs:170

    IAzureContainerRegistryResource? IAzureComputeEnvironmentResource.ContainerRegistry => ContainerRegistry;

    /// <summary>
    /// Gets the Azure Container Registry resource used by this project.
    /// </summary>
    public AzureContainerRegistryResource? ContainerRegistry
    {
        get
        {
            var registry = GetContainerRegistry();

            if (registry is null)
            {
                return null;
            }

            if (registry is not AzureContainerRegistryResource azureRegistry)
            {
                throw new InvalidOperationException(
                    $"The container registry configured for the Azure Cognitive Services project '{Name}' is not an Azure Container Registry. " +
                    $"Only Azure Container Registry resources are supported. Use '.WithAzureContainerRegistry()' to configure an Azure Container Registry.");
            }

            return azureRegistry;
        }
    }

    private IContainerRegistry? GetContainerRegistry()
    {
        if (this.TryGetLastAnnotation<ContainerRegistryReferenceAnnotation>(out var annotation))
        {
            return annotation.Registry;
        }
        return DefaultContainerRegistry;
    }

    /// <summary>

View on GitHub (pinned to 25830f84bd)