{"record":{"id":"9fb3d733b12c6a68","repo":"microsoft/aspire","slug":"could-not-create-http-health-check-for-resource-builder","errorCode":null,"errorMessage":"Could not create HTTP health check for resource '{builder.Resource.Name}' as the endpoint selector returned null.","messagePattern":"Could not create HTTP health check for resource '(.+?)' as the endpoint selector returned null\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ResourceBuilderExtensions.cs","lineNumber":2809,"sourceCode":"    /// reporting a healthy status based on the return code returned from the\n    /// \"/health\" path on the backend server.\n    /// <code lang=\"C#\">\n    /// var builder = DistributedApplication.CreateBuilder(args);\n    /// var backend = builder.AddProject&lt;Projects.Backend&gt;(\"backend\");\n    /// backend.WithHttpHealthCheck(() => backend.GetEndpoint(\"https\"), path: \"/health\")\n    /// builder.AddProject&lt;Projects.Frontend&gt;(\"frontend\")\n    ///        .WithReference(backend).WaitFor(backend);\n    /// </code>\n    /// </example>\n    /// <para>This method is not available in polyglot app hosts. Use the endpointName-based overload instead.</para>\n    /// </remarks>\n    [AspireExportIgnore(Reason = \"Func<EndpointReference> delegate — not ATS-compatible.\")]\n    public static IResourceBuilder<T> WithHttpHealthCheck<T>(this IResourceBuilder<T> builder, Func<EndpointReference>? endpointSelector, string? path = null, int? statusCode = null) where T : IResourceWithEndpoints\n    {\n        endpointSelector ??= DefaultEndpointSelector(builder);\n\n        var endpoint = endpointSelector()\n            ?? throw new DistributedApplicationException($\"Could not create HTTP health check 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 health check for resource '{builder.Resource.Name}' as the endpoint with name '{endpoint.EndpointName}' and scheme '{endpoint.Scheme}' is not an HTTP endpoint.\");\n        }\n\n        path ??= \"/\";\n        statusCode ??= 200;\n\n        var endpointName = endpoint.EndpointName;\n\n        // Validate that the endpoint exists during allocation to fail fast on misconfiguration.\n        builder.OnResourceEndpointsAllocated((_, @event, ct) =>\n        {\n            if (!endpoint.Exists)\n            {\n                throw new DistributedApplicationException($\"The endpoint '{endpointName}' does not exist on the resource '{builder.Resource.Name}'.\");\n            }","sourceCodeStart":2791,"sourceCodeEnd":2827,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ResourceBuilderExtensions.cs#L2791-L2827","documentation":"Thrown by WithHttpHealthCheck when the supplied endpoint selector (or the default selector) returns null, so no endpoint exists to probe. Aspire needs a concrete EndpointReference to build the HTTP health check.","triggerScenarios":"Passing a custom Func<EndpointReference>? endpointSelector that returns null, or relying on the default selector when the resource has no endpoint matching the expected name/scheme.","commonSituations":"Calling WithHttpHealthCheck on a resource that has no WithEndpoint/WithHttpEndpoint defined yet; a typo'd endpoint name in a lambda; the endpoint is added conditionally at runtime so the selector resolves null.","solutions":["Add an endpoint to the resource first (WithEndpoint/WithHttpEndpoint) before wiring the health check.","Fix the custom selector to return a valid EndpointReference instead of null.","Pass the endpoint name explicitly: WithHttpHealthCheck(\"http\") so the default selector finds it.","Verify ordering: endpoints declared before WithHttpHealthCheck in the builder chain."],"exampleFix":"// before\nvar api = builder.AddProject<Projects.Api>(\"api\");\napi.WithHttpHealthCheck(e => null); // throws\n\n// after\nvar api = builder.AddProject<Projects.Api>(\"api\")\n    .WithHttpEndpoint(name: \"http\");\napi.WithHttpHealthCheck(\"http\");","handlingStrategy":"validation","validationCode":"var endpoints = resource.Annotations.OfType<EndpointAnnotation>().ToList();\nif (endpoints.Count == 0)\n    throw new InvalidOperationException(\"Add an endpoint (WithHttpEndpoint/WithEndpoint) before WithHttpHealthCheck.\");","typeGuard":"bool HasHttpEndpoint(IResource r) => r.Annotations.OfType<EndpointAnnotation>().Any(e => e.Scheme is \"http\" or \"https\");","tryCatchPattern":"try { api.WithHttpHealthCheck(\"http\"); }\ncatch (DistributedApplicationException ex) when (ex.Message.Contains(\"endpoint selector returned null\"))\n{ Console.Error.WriteLine(\"Declare the endpoint first.\"); throw; }","preventionTips":["Always declare endpoints before wiring HTTP health checks.","Prefer the string endpoint-name overload over custom lambdas.","Keep endpoint creation unconditional or mirror conditions in health-check wiring."],"tags":["aspire","health-check","endpoint","null-reference"],"backgroundTag":"null-argument","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"}