{"record":{"id":"6b22a2295de5ad43","repo":"microsoft/aspire","slug":"unable-to-create-hosted-agent-for-resource-resource-name-6b22a2","errorCode":null,"errorMessage":"Unable to create hosted agent for resource '{resource.Name}' because it is not a container, executable, or project resource.","messagePattern":"Unable to create hosted agent for resource '(.+?)' because it is not a container, executable, or project resource\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs","lineNumber":498,"sourceCode":"            builder.ApplicationBuilder.CreateResourceBuilder(executableResource)\n                .PublishAsDockerFile();\n\n            if (builder.ApplicationBuilder.TryCreateResourceBuilder(resource.Name, out containerResourceBuilder))\n            {\n                target = containerResourceBuilder.Resource;\n            }\n            else\n            {\n                throw new InvalidOperationException($\"Unable to create hosted agent for resource '{resource.Name}' because it could not be converted to a container resource.\");\n            }\n        }\n        else if (resource is ProjectResource)\n        {\n            target = resource;\n        }\n        else\n        {\n            throw new InvalidOperationException($\"Unable to create hosted agent for resource '{resource.Name}' because it is not a container, executable, or project resource.\");\n        }\n\n        EnsureDefaultHostedAgentEndpoint(builder, target);\n\n        if (target is ProjectResource projectTarget)\n        {\n            // Foundry hosted agents are containerized and the platform owns the listening port contract.\n            // Keep the user's local endpoint metadata intact, but do not emit project endpoint variables\n            // such as ASPNETCORE_URLS/HTTP_PORTS because they require EndpointProperty.TargetPort, which\n            // Foundry hosted-agent deployment endpoints intentionally do not support.\n            builder.ApplicationBuilder.CreateResourceBuilder(projectTarget)\n                .WithEndpointsInEnvironment(_ => false);\n        }\n\n        // The hosted agent wrapper is not the deployed workload. Apply the Foundry\n        // reference to the target so its connection annotations flow into the deployment.\n        builder.ApplicationBuilder.CreateResourceBuilder(target)\n            .WithReference(project);","sourceCodeStart":480,"sourceCodeEnd":516,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L480-L516","documentation":"ConfigurePublishMode only supports converting container, executable, and project resources into hosted agents; any other resource type reaches the final else branch and throws this InvalidOperationException. The library cannot represent arbitrary resources as Foundry hosted agents.","triggerScenarios":"Calling ConfigureAsHostedAgent on a resource that is not a ContainerResource, ExecutableResource, or ProjectResource (e.g. a plain CustomResource, connection-string resource, or parameter resource).","commonSituations":"Applying the hosted-agent extension to the wrong builder (e.g. a database or parameter resource instead of the app service itself); refactoring that changed a resource's type; applying a generic extension to all resources in a loop.","solutions":["Call ConfigureAsHostedAgent only on container, executable, or project resources","Move the extension call to the correct resource builder (the agent's host app, not its dependencies)","If the resource is a custom type, wrap it in a container or project resource first","Check loops/composition helpers that may apply the extension to every resource"],"exampleFix":"// before\nbuilder.AddRedis(\"cache\").ConfigureAsHostedAgent(project, ...); // unsupported type\n// after\nbuilder.AddProject<Projects.AgentHost>(\"agent-host\").ConfigureAsHostedAgent(project, ...);","handlingStrategy":"validation","validationCode":"if (resource is not (ContainerResource or ExecutableResource or ProjectResource))\n    throw new InvalidOperationException($\"'{resource.Name}' must be a container, executable, or project resource to be a hosted agent.\");","typeGuard":"static bool CanBeHostedAgent(IResource r) => r is ContainerResource or ExecutableResource or ProjectResource;","tryCatchPattern":"try\n{\n    builder.ConfigureAsHostedAgent(project, protocol, ...);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not a container, executable, or project resource\"))\n{\n    logger.LogError(ex, \"Apply ConfigureAsHostedAgent to the app resource, not its dependencies.\");\n    throw;\n}","preventionTips":["Only call ConfigureAsHostedAgent on container, executable, or project resource builders","Apply the extension to the agent host app, not to databases/parameters/infrastructure resources","Use the type guard before composing generic extension helpers"],"tags":["azure","foundry","publish","resource-type"],"backgroundTag":"unsupported-operation","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"}