{"record":{"id":"823e882b3ff68992","repo":"microsoft/aspire","slug":"compute-environment-for-resource-target-name-must-be-an","errorCode":null,"errorMessage":"Compute environment for resource '{Target.Name}' must be an AzureCognitiveServicesProjectResource to deploy as hosted agent.","messagePattern":"Compute environment for resource '(.+?)' must be an AzureCognitiveServicesProjectResource to deploy as hosted agent\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs","lineNumber":45,"sourceCode":"{\n    internal const string DefaultResponsesProtocolVersion = \"2.0.0\";\n\n    /// <summary>\n    /// Creates a new instance of the <see cref=\"AzureHostedAgentResource\"/> class.\n    /// </summary>\n    public AzureHostedAgentResource([ResourceName] string name, IResource target, Action<HostedAgentConfiguration>? configure = null) : base(name)\n    {\n        ArgumentNullException.ThrowIfNull(target);\n        Target = target;\n        Configure = configure;\n        Annotations.Add(new ManifestPublishingCallbackAnnotation(PublishAsync));\n        // Set up steps for deploying this particular hosted agent\n        Annotations.Add(new PipelineStepAnnotation(async (ctx) =>\n        {\n            List<PipelineStep> steps = [];\n            var deploymentAnnotation = Target.GetDeploymentTargetAnnotation() ?? throw new InvalidOperationException($\"Deployment target annotation is required on resource '{Target.Name}' to deploy as hosted agent.\");\n            var project = deploymentAnnotation.ComputeEnvironment as AzureCognitiveServicesProjectResource\n                ?? throw new InvalidOperationException($\"Compute environment for resource '{Target.Name}' must be an AzureCognitiveServicesProjectResource to deploy as hosted agent.\");\n\n            // Create a step to deploy container as agent\n            var agentDeployStep = new PipelineStep\n            {\n                Name = $\"deploy-{Name}\",\n                Action = async (ctx) =>\n                {\n                    var version = await DeployAsync(ctx, project).ConfigureAwait(false);\n                    ctx.ReportingStep.Log(LogLevel.Information, new MarkdownString($\"Successfully deployed **{Name}** as Hosted Agent (version {version})\"));\n                    Version.Set(version.Version);\n                },\n                Tags = [WellKnownPipelineTags.DeployCompute],\n                RequiredBySteps = [WellKnownPipelineSteps.Deploy],\n                Resource = this,\n                DependsOnSteps = [WellKnownPipelineSteps.DeployPrereq, AzureEnvironmentResource.ProvisionInfrastructureStepName]\n            };\n            steps.Add(agentDeployStep);\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs#L27-L63","documentation":"The hosted agent's deployment target annotation exists but its ComputeEnvironment is not an AzureCognitiveServicesProjectResource. AzureHostedAgentResource can only deploy agents into an Azure AI Foundry (Cognitive Services) project, so the pipeline step annotation casts the compute environment and throws this InvalidOperationException when the cast fails.","triggerScenarios":"The target resource's deployment target annotation was created by a different environment type (e.g. Azure Container Apps environment, plain container environment), so `deploymentAnnotation.ComputeEnvironment as AzureCognitiveServicesProjectResource` yields null while running publish/deploy.","commonSituations":"The target project/container is registered in a ContainerApp environment (or another publish environment) instead of a Foundry project environment; the app model has multiple environments and the target picked up the wrong one; copy-pasted AppHost code wires the agent to a resource deployed to ACA.","solutions":["Move the target resource into an AzureCognitiveServicesProjectResource-based environment (the Foundry project's compute environment) so its deployment annotation's ComputeEnvironment is a Foundry project","Remove the target from the wrong compute environment (e.g. ACA environment) and re-add it to the Foundry project environment","Verify with `Target.GetDeploymentTargetAnnotation()?.ComputeEnvironment` in a debug session which environment the target is actually bound to"],"exampleFix":"// before: target deployed to ACA environment\nvar aca = builder.AddAzureContainerAppEnvironment(\"env\");\nvar agent = builder.AddHostedAgent(\"my-agent\", projectIn(aca));\n// after: target deployed in the Foundry project environment\nvar foundry = builder.AddAzureCognitiveServicesProject(\"foundry\");\nvar agent = builder.AddHostedAgent(\"my-agent\", projectIn(foundry));","handlingStrategy":"validation","validationCode":"// Verify the compute environment type before deploy:\nvar annotation = target.GetDeploymentTargetAnnotation();\nvar isFoundryProject = annotation?.ComputeEnvironment is AzureCognitiveServicesProjectResource;\nif (!isFoundryProject)\n{\n    throw new InvalidOperationException($\"{target.Name} is bound to {annotation?.ComputeEnvironment?.GetType().Name ?? \"no\"} environment; a Foundry project environment is required.\");\n}","typeGuard":"bool IsFoundryHostedTarget(IResource target) =>\n    target.GetDeploymentTargetAnnotation()?.ComputeEnvironment is AzureCognitiveServicesProjectResource;","tryCatchPattern":"try\n{\n    // run deploy pipeline\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"must be an AzureCognitiveServicesProjectResource\"))\n{\n    // re-wire the target resource into the Foundry project environment\n}","preventionTips":["Use one compute environment per deployment story; keep hosted-agent targets only in Foundry project environments","When multiple environments exist, explicitly attach each target to the intended one instead of relying on defaults","Code-review AppHost changes that move resources between environments when hosted agents are involved"],"tags":["azure","foundry","deploy","compute-environment","type-mismatch"],"backgroundTag":"incompatible-source-type","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"}