{"record":{"id":"0989cce8d80c58f1","repo":"microsoft/aspire","slug":"unable-to-create-hosted-agent-for-resource-resource-name","errorCode":null,"errorMessage":"Unable to create hosted agent for resource '{resource.Name}' because it could not be converted to a container resource.","messagePattern":"Unable to create hosted agent for resource '(.+?)' because it could not be converted to a container resource\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs","lineNumber":489,"sourceCode":"        }\n        else if (builder.ApplicationBuilder.TryCreateResourceBuilder<ContainerResource>(resource.Name, out var containerResourceBuilder))\n        {\n            target = containerResourceBuilder.Resource;\n        }\n        else if (resource is ExecutableResource executableResource)\n        {\n            // Ensure we have a container resource to deploy.\n            // ExecutableResource needs PublishAsDockerFile() to convert it into a container resource at this stage.\n            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","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs#L471-L507","documentation":"During publish-mode configuration, ConfigurePublishMode attempts to obtain a builder for the target resource so it can be converted to a container-backed hosted agent. If the resource is a container resource but TryCreateResourceBuilder cannot create a builder for it, the method throws this InvalidOperationException.","triggerScenarios":"Calling ConfigureAsHostedAgent on a resource that is a container resource whose builder cannot be recreated by TryCreateResourceBuilder (e.g. resource was added outside the application builder's tracked resources, or a custom container-derived resource with an unresolvable name).","commonSituations":"Custom resource types deriving from container semantics that the extension cannot map; resources added via unusual/custom builder paths; duplicate or mutated resource names breaking the builder lookup.","solutions":["Ensure the resource was added via the standard IResourceBuilder APIs on the same ApplicationBuilder","Convert the resource to a standard ContainerResource usage or a project/executable resource supported by hosted agents","Check that the resource name matches the one registered in the ApplicationBuilder","If using a custom resource type, implement conversion to ContainerResource or register it in a way TryCreateResourceBuilder can resolve"],"exampleFix":"// before\nvar custom = new MyWeirdContainer(\"agent\");\nbuilder.AddResource(custom).ConfigureAsHostedAgent(...); // builder lookup fails\n// after\nbuilder.AddContainer(\"agent\", image, tag).ConfigureAsHostedAgent(project, ...);","handlingStrategy":"validation","validationCode":"if (resource is ContainerResource && !builder.ApplicationBuilder.TryCreateResourceBuilder(resource.Name, out _))\n    throw new InvalidOperationException($\"Resource '{resource.Name}' must be registered via the standard ApplicationBuilder to become a hosted agent.\");","typeGuard":"static bool IsHostedAgentConvertible(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(\"could not be converted to a container resource\"))\n{\n    logger.LogError(ex, \"Resource must be added via standard builder APIs.\");\n    throw;\n}","preventionTips":["Add resources with standard AddContainer/AddProject/AddExecutable builders on the same ApplicationBuilder","Avoid custom container-derived resource types for hosted agents","Keep resource names unique and unmutated"],"tags":["azure","foundry","publish","container"],"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"}