{"record":{"id":"bd6a15b43c9d5e4b","repo":"microsoft/aspire","slug":"could-not-create-http-command-for-resource-builder-resource","errorCode":null,"errorMessage":"Could not create HTTP command for resource '{builder.Resource.Name}' as the endpoint selector returned null.","messagePattern":"Could not create HTTP command for resource '(.+?)' as the endpoint selector returned null\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ResourceBuilderExtensions.cs","lineNumber":3684,"sourceCode":"    /// loadGenerator.WithReference(customerService);\n    /// </code>\n    /// </example>\n    /// <para>This method is not available in polyglot app hosts.</para>\n    /// </remarks>\n    [AspireExportIgnore(Reason = \"Use the ATS-specific withHttpCommand export.\")]\n    public static IResourceBuilder<TResource> WithHttpCommand<TResource>(\n        this IResourceBuilder<TResource> builder,\n        string path,\n        string displayName,\n        Func<EndpointReference>? endpointSelector,\n        string? commandName = null,\n        HttpCommandOptions? commandOptions = null)\n        where TResource : IResourceWithEndpoints\n    {\n        endpointSelector ??= DefaultEndpointSelector(builder);\n\n        var endpoint = endpointSelector()\n            ?? throw new DistributedApplicationException($\"Could not create HTTP command for resource '{builder.Resource.Name}' as the endpoint selector returned null.\");\n\n        if (endpoint.Scheme != \"http\" && endpoint.Scheme != \"https\")\n        {\n            throw new DistributedApplicationException($\"Could not create HTTP command for resource '{builder.Resource.Name}' as the endpoint with name '{endpoint.EndpointName}' and scheme '{endpoint.Scheme}' is not an HTTP endpoint.\");\n        }\n\n        builder.ApplicationBuilder.Services.AddHttpClient();\n\n        commandOptions ??= HttpCommandOptions.Default;\n        commandOptions.Method ??= HttpMethod.Post;\n\n        commandName ??= $\"{endpoint.Resource.Name}-{endpoint.EndpointName}-http-{commandOptions.Method.Method.ToLowerInvariant()}-{path}\";\n\n        if (commandOptions.UpdateState is null)\n        {\n            commandOptions.UpdateState = context =>\n            {\n                var resourceState = context.ResourceSnapshot.State?.Text;","sourceCodeStart":3666,"sourceCodeEnd":3702,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ResourceBuilderExtensions.cs#L3666-L3702","documentation":"HTTP resource commands (e.g. start/stop-style commands over HTTP) obtain their target endpoint by invoking a user-supplied or default endpoint selector. If the selector returns null there is no endpoint to send the request to, so Aspire throws DistributedApplicationException naming the resource.","triggerScenarios":"Passing a custom HttpCommandOptions.EndpointSelector (or equivalent selector parameter) whose function returns null — e.g. it filters endpoints by scheme/name and no endpoint matches; calling the API before the resource has any endpoint annotations.","commonSituations":"Selector logic that assumes an endpoint named 'https' exists when only 'http' is defined; endpoints added conditionally so they are missing in some run modes; running the command on a resource that never got an WithEndpoint call.","solutions":["Add or verify the resource declares an endpoint (WithEndpoint/WithHttpEndpoint) before the HTTP command is created","Fix the custom selector so it returns an endpoint reference instead of null (check name/scheme filters)","Add a fallback in the selector, e.g. endpoints.FirstOrDefault(e => e.Scheme == \"http\") ?? endpoints.First()"],"exampleFix":"// before\nselector: () => endpoints.FirstOrDefault(e => e.EndpointName == \"https\") // null when only http exists\n// after\nselector: () => endpoints.FirstOrDefault(e => e.EndpointName == \"https\")\n       ?? endpoints.First(e => e.Scheme == \"http\");","handlingStrategy":"type-guard","validationCode":"var endpoints = builder.Resource.GetEndpoints().ToList();\nif (endpoints.Count == 0) throw new InvalidOperationException(\"Resource has no endpoints for HTTP command\");","typeGuard":"bool SelectorResolves(Func<EndpointReference?> selector) => selector() is not null;","tryCatchPattern":"try { builder.WithHttpCommand(...); }\ncatch (DistributedApplicationException ex) when (ex.Message.Contains(\"endpoint selector returned null\")) { /* fix selector or add endpoint */ }","preventionTips":["Make custom endpoint selectors always fall back to a default endpoint","Declare endpoints before creating HTTP commands that reference them"],"tags":["http","endpoint-selector","null-return","aspire-hosting"],"backgroundTag":"null-argument","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"}