microsoft/aspire · error · InvalidOperationException
The Container Registry associated with resource
Error message
The Container Registry associated with resource '{builder.Resource.Name}' is not an AzureContainerRegistryResource. What it means
GetAzureContainerRegistry casts the associated ContainerRegistry to AzureContainerRegistryResource and throws InvalidOperationException when the object is an IContainerRegistry implementation of a different type (e.g. an existing/Bring-Your-Own registry resource rather than an Aspire-provisioned one).
Solutions
- Provision the registry via AddAzureContainerRegistry so the associated type is AzureContainerRegistryResource
- Use the existing-registry API appropriate for BYO registries instead of this getter
- Cast/check ContainerRegistry type before calling and handle the alternate type
Example fix
// before
var env = builder.AddAzureContainerAppEnvironment("env").WithExistingRegistry(existing);
var registry = env.GetAzureContainerRegistry();
// after
var env = builder.AddAzureContainerAppEnvironment("env");
env.AddAzureContainerRegistry("myregistry");
var registry = env.GetAzureContainerRegistry(); Defensive patterns
Strategy: type-guard
Validate before calling
if (env.Resource.ContainerRegistry is not AzureContainerRegistryResource)
throw new InvalidOperationException("Environment is associated with a non-Aspire registry."); Type guard
var registry = env.Resource.ContainerRegistry as AzureContainerRegistryResource;
if (registry is null) { /* use existing-registry APIs instead */ } Try / catch
try { var registry = env.GetAzureContainerRegistry(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("is not an Azure Container Registry")) { log.LogError(ex, "Registry type mismatch on {Env}", env.Resource.Name); } Prevention
- Provision registries via AddAzureContainerRegistry rather than BYO when using this getter
- Check the concrete type of the associated registry
- Use the existing-registry APIs for BYO registries
When it happens
Trigger: Calling GetAzureContainerRegistry on a compute environment whose ContainerRegistry property is set to a non-AzureContainerRegistryResource implementation of IContainerRegistry.
Common situations: Wiring the environment to an existing registry by name/connection string instead of an Aspire-provisioned AzureContainerRegistryResource; mixing registry types from different hosting integrations.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- The container registry configured for the Azure Cognitive…
- The container registry configured for the Azure App Service…
- The resource ' ' does not have an associated Azure…
- A purge task with the name
- A of type cannot be assigned to a BicepValue< >.
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/641212e1acfe7b9b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs:119
/// <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
/// based on existing purge tasks to avoid conflicts. If a task with the specified name already exists,View on GitHub (pinned to 25830f84bd)