{"record":{"id":"661bcc55c34ebce0","repo":"microsoft/aspire","slug":"resource-resource-name-has-multiple-compute-environments","errorCode":null,"errorMessage":"Resource '{resource.Name}' has multiple compute environments - '{computeEnvironmentNames}'. Please specify a single compute environment using 'WithComputeEnvironment'.","messagePattern":"Resource '(.+?)' has multiple compute environments - '(.+?)'\\. Please specify a single compute environment using 'WithComputeEnvironment'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs","lineNumber":1082,"sourceCode":"            }\n\n            // If the resource is bound to a specific compute environment, use that one.\n            selectedComputeEnvironment = computeEnvironmentAnnotation.ComputeEnvironment;\n        }\n\n        if (resource.TryGetAnnotationsOfType<DeploymentTargetAnnotation>(out var deploymentTargetAnnotations))\n        {\n            var annotations = deploymentTargetAnnotations.ToArray();\n\n            if (selectedComputeEnvironment is not null)\n            {\n                return annotations.SingleOrDefault(a => a.ComputeEnvironment == selectedComputeEnvironment);\n            }\n\n            if (annotations.Length > 1)\n            {\n                var computeEnvironmentNames = string.Join(\", \", annotations.Select(a => a.ComputeEnvironment?.Name));\n                throw new InvalidOperationException($\"Resource '{resource.Name}' has multiple compute environments - '{computeEnvironmentNames}'. Please specify a single compute environment using 'WithComputeEnvironment'.\");\n            }\n\n            var deploymentTargetAnnotation = annotations[0];\n\n            // If you have a DeploymentTargetAnnotation, it means the resource is bound to a specific compute environment.\n            // Skip the annotation if it doesn't match the specified targetComputeEnvironment.\n            if (targetComputeEnvironment is not null && targetComputeEnvironment != deploymentTargetAnnotation.ComputeEnvironment)\n            {\n                return null;\n            }\n\n            return deploymentTargetAnnotation;\n        }\n        return null;\n    }\n\n    /// <summary>\n    /// Gets the lifetime type for the specified resource.","sourceCodeStart":1064,"sourceCodeEnd":1100,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs#L1064-L1100","documentation":"During publish/deploy, each resource must resolve to exactly one compute environment's DeploymentTargetAnnotation. If a resource has annotations from more than one compute environment and none was explicitly selected via WithComputeEnvironment, GetDeploymentTargetAnnotation throws because the target is ambiguous.","triggerScenarios":"Binding a resource to two or more compute environments (e.g. both ACA and Kubernetes environments in the same app model) without calling WithComputeEnvironment to pick one, then publishing.","commonSituations":"Multi-target publish setups where a resource is added to multiple environments (e.g. Azure App Service + ACA) for comparison, forgetting to disambiguate before deployment.","solutions":["Call WithComputeEnvironment on the resource to select a single compute environment.","Remove one of the conflicting environment bindings from the resource.","Filter annotations to the environment you publish for before resolving the deployment target."],"exampleFix":"// before\nvar aca = builder.AddAzureContainerAppEnvironment(\"aca\");\nvar aks = builder.AddKubernetesEnvironment(\"aks\");\nvar redis = builder.AddRedis(\"cache\");\nredis.WithComputeEnvironment(aca).WithComputeEnvironment(aks); // two environments\n// after\nredis.WithComputeEnvironment(aca); // select exactly one environment","handlingStrategy":"validation","validationCode":"var envCount = resource.Annotations.OfType<DeploymentTargetAnnotation>().Select(a => a.ComputeEnvironment).Distinct().Count();\nif (envCount > 1) throw new InvalidOperationException(\"Select a single compute environment with WithComputeEnvironment before publishing\");","typeGuard":null,"tryCatchPattern":"try { var target = resource.GetDeploymentTargetAnnotation(computeEnvironment); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"multiple compute environments\")) { /* prompt for explicit environment */ }","preventionTips":["Bind each resource to exactly one compute environment unless multi-environment publishing is intended.","Always call WithComputeEnvironment when a resource participates in more than one environment.","Standardize environment selection in a shared publish setup method."],"tags":["aspire","publish","compute-environment","ambiguity"],"backgroundTag":"conflicting-config-options","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"}