{"record":{"id":"4d2ed5bf8453eb8f","repo":"microsoft/aspire","slug":"the-resource-builder-resource-name-does-not-have-an","errorCode":null,"errorMessage":"The resource '{builder.Resource.Name}' does not have an associated Azure Container Registry.","messagePattern":"The resource '(.+?)' does not have an associated Azure Container Registry\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs","lineNumber":118,"sourceCode":"\n    /// <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","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs#L100-L136","documentation":"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.","triggerScenarios":"Calling GetAzureContainerRegistry on a compute environment resource that was created without an accompanying container registry, or before registry association was configured.","commonSituations":"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.","solutions":["Associate a container registry with the compute environment before calling the getter (e.g. AddAzureContainerRegistry on the environment)","Call GetAzureContainerRegistry only on environment resources known to have a registry (check Resource.ContainerRegistry first)","Verify you are calling it in run/publish phase after model enrichment has run"],"exampleFix":"// before\nvar registry = builder.GetAzureContainerRegistry();\n// after\nvar env = builder.AddAzureContainerAppEnvironment(\"env\").WithAzureContainerRegistry();\nvar registry = env.GetAzureContainerRegistry();","handlingStrategy":"type-guard","validationCode":"if (env.Resource.ContainerRegistry is null)\n    throw new InvalidOperationException(\"Associate a registry with the environment before calling GetAzureContainerRegistry.\");","typeGuard":"var registry = env.Resource.ContainerRegistry as AzureContainerRegistryResource;\nif (registry is null) { /* handle missing or wrong-type registry */ }","tryCatchPattern":"try { var registry = env.GetAzureContainerRegistry(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not have an associated Azure Container Registry\")) { log.LogError(ex, \"No registry on {Env}\", env.Resource.Name); }","preventionTips":["Always call AddAzureContainerRegistry on the environment before fetching it","Check Resource.ContainerRegistry for null in app-model code","Understand the run/publish phase in which enrichment runs"],"tags":["azure","container-registry","invalid-state","aspire"],"backgroundTag":"invalid-state-transition","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}