{"record":{"id":"641212e1acfe7b9b","repo":"microsoft/aspire","slug":"the-container-registry-associated-with-resource-builder","errorCode":null,"errorMessage":"The Container Registry associated with resource '{builder.Resource.Name}' is not an AzureContainerRegistryResource.","messagePattern":"The Container Registry associated with resource '(.+?)' is not an AzureContainerRegistryResource\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs","lineNumber":119,"sourceCode":"    /// <summary>\n    /// Gets the <see cref=\"AzureContainerRegistryResource\"/> associated with the specified Azure compute environment resource.\n    /// </summary>\n    /// <ats-summary>Gets the Azure Container Registry associated with a compute environment resource.</ats-summary>\n    /// <typeparam name=\"T\">The resource type that implements <see cref=\"IAzureComputeEnvironmentResource\"/>.</typeparam>\n    /// <param name=\"builder\">The resource builder for the compute environment resource.</param>\n    /// <returns>A reference to the <see cref=\"IResourceBuilder{AzureContainerRegistryResource}\"/> for the associated registry.</returns>\n    /// <ats-returns>The resource builder.</ats-returns>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"builder\"/> is <see langword=\"null\"/>.</exception>\n    /// <exception cref=\"InvalidOperationException\">Thrown when the resource does not have an associated Azure Container Registry,\n    /// or when the associated container registry is not an <see cref=\"AzureContainerRegistryResource\"/>.</exception>\n    [AspireExport]\n    public static IResourceBuilder<AzureContainerRegistryResource> GetAzureContainerRegistry<T>(this IResourceBuilder<T> builder)\n        where T : IResource, IAzureComputeEnvironmentResource\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n\n        var containerRegistry = builder.Resource.ContainerRegistry ?? throw new InvalidOperationException($\"The resource '{builder.Resource.Name}' does not have an associated Azure Container Registry.\");\n        var registry = containerRegistry as AzureContainerRegistryResource ?? throw new InvalidOperationException($\"The Container Registry associated with resource '{builder.Resource.Name}' is not an Azure Container Registry.\");\n\n        return builder.ApplicationBuilder.CreateResourceBuilder(registry);\n    }\n\n    /// <summary>\n    /// Adds a scheduled ACR purge task to remove old or unused container images from the registry.\n    /// </summary>\n    /// <param name=\"builder\">The resource builder for the <see cref=\"AzureContainerRegistryResource\"/>.</param>\n    /// <param name=\"schedule\">The cron schedule for the purge task timer trigger. Must be a five-part cron expression\n    /// (<c>minute hour day-of-month month day-of-week</c>); seconds are not supported.</param>\n    /// <param name=\"filter\">An optional filter for the <c>acr purge --filter</c> parameter. Only repositories matching this\n    /// filter will be purged. Defaults to <c>\".*:.*\"</c> (all repositories and tags) when <see langword=\"null\"/>.</param>\n    /// <param name=\"ago\">The age threshold for <c>acr purge --ago</c>. Images older than this duration will be considered\n    /// for removal. Uses Go-style duration format (e.g., <c>2d3h6m</c>). Defaults to <c>0d</c> when <see langword=\"null\"/>.</param>\n    /// <param name=\"keep\">The number of most recent images to keep per repository, regardless of age.\n    /// Must be greater than zero. Defaults to 3.</param>\n    /// <param name=\"taskName\">An optional name for the ACR task resource. If not provided, a name is auto-generated\n    /// based on existing purge tasks to avoid conflicts. If a task with the specified name already exists,","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs#L101-L137","documentation":"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).","triggerScenarios":"Calling GetAzureContainerRegistry on a compute environment whose ContainerRegistry property is set to a non-AzureContainerRegistryResource implementation of IContainerRegistry.","commonSituations":"Wiring the environment to an existing registry by name/connection string instead of an Aspire-provisioned AzureContainerRegistryResource; mixing registry types from different hosting integrations.","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"],"exampleFix":"// before\nvar env = builder.AddAzureContainerAppEnvironment(\"env\").WithExistingRegistry(existing);\nvar registry = env.GetAzureContainerRegistry();\n// after\nvar env = builder.AddAzureContainerAppEnvironment(\"env\");\nenv.AddAzureContainerRegistry(\"myregistry\");\nvar registry = env.GetAzureContainerRegistry();","handlingStrategy":"type-guard","validationCode":"if (env.Resource.ContainerRegistry is not AzureContainerRegistryResource)\n    throw new InvalidOperationException(\"Environment is associated with a non-Aspire registry.\");","typeGuard":"var registry = env.Resource.ContainerRegistry as AzureContainerRegistryResource;\nif (registry is null) { /* use existing-registry APIs instead */ }","tryCatchPattern":"try { var registry = env.GetAzureContainerRegistry(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"is not an Azure Container Registry\")) { log.LogError(ex, \"Registry type mismatch on {Env}\", env.Resource.Name); }","preventionTips":["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"],"tags":["azure","container-registry","type-mismatch","aspire"],"backgroundTag":"type-mismatch","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}