{"record":{"id":"210382a39b94825d","repo":"dotnet/yarp","slug":"routing-endpoint-wasn-t-set-for-the-current-reques","errorCode":null,"errorMessage":"Routing Endpoint wasn't set for the current request.","messagePattern":"Routing Endpoint wasn't set for the current request\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/ReverseProxy/Model/ProxyPipelineInitializerMiddleware.cs","lineNumber":39,"sourceCode":"    private readonly ILogger _logger;\n    private readonly RequestDelegate _next;\n    private readonly IOptionsMonitor<RequestTimeoutOptions> _timeoutOptions;\n\n    public ProxyPipelineInitializerMiddleware(RequestDelegate next, ILogger<ProxyPipelineInitializerMiddleware> logger, IOptionsMonitor<RequestTimeoutOptions> timeoutOptions)\n    {\n        ArgumentNullException.ThrowIfNull(logger);\n        ArgumentNullException.ThrowIfNull(next);\n        ArgumentNullException.ThrowIfNull(timeoutOptions);\n\n        _logger = logger;\n        _next = next;\n        _timeoutOptions = timeoutOptions;\n    }\n\n    public Task Invoke(HttpContext context)\n    {\n        var endpoint = context.GetEndpoint()\n           ?? throw new InvalidOperationException($\"Routing Endpoint wasn't set for the current request.\");\n\n        var route = endpoint.Metadata.GetMetadata<RouteModel>()\n            ?? throw new InvalidOperationException($\"Routing Endpoint is missing {typeof(RouteModel).FullName} metadata.\");\n\n        var cluster = route.Cluster;\n        // TODO: Validate on load https://github.com/dotnet/yarp/issues/797\n        if (cluster is null)\n        {\n            Log.NoClusterFound(_logger, route.Config.RouteId);\n            context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;\n            return Task.CompletedTask;\n        }\n\n        EnsureRequestTimeoutPolicyIsAppliedCorrectly(context, endpoint, route);\n\n        var destinationsState = cluster.DestinationsState;\n        context.Features.Set<IReverseProxyFeature>(new ReverseProxyFeature\n        {","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/dotnet/yarp/blob/bd11867bee7df522e7fd3effb08a9c85fd616908/src/ReverseProxy/Model/ProxyPipelineInitializerMiddleware.cs#L21-L57","documentation":"The `ProxyPipelineInitializerMiddleware` expects ASP.NET Core endpoint routing to have resolved an endpoint for the current request. It retrieves the endpoint via `context.GetEndpoint()`. If null, it means routing middleware (`UseRouting`) has not run, or this YARP middleware is placed in the pipeline before endpoint resolution. The proxy cannot proceed without knowing which route matched.","triggerScenarios":"`ProxyPipelineInitializerMiddleware.Invoke` is called but `context.GetEndpoint()` returns null. This happens when `UseRouting()` was not called before the proxy middleware, or the middleware was accidentally added outside the endpoint-mapped pipeline (e.g., as a bare `app.UseMiddleware<ProxyPipelineInitializerMiddleware>()`).","commonSituations":"A developer removes `app.UseRouting()` while refactoring, not realizing YARP depends on endpoint routing. The YARP middleware is registered before `UseRouting` in the pipeline ordering. A misconfigured custom pipeline adds the proxy middleware at the wrong position.","solutions":["Ensure `app.UseRouting()` is called before `app.MapReverseProxy()` in the middleware pipeline.","Verify `MapReverseProxy()` is called inside `app.UseEndpoints(...)` (or uses the modern minimal `app.MapReverseProxy()` which handles this internally).","Check that no middleware between `UseRouting` and the endpoint short-circuits the request before endpoint selection.","If using a custom middleware pipeline inside `MapReverseProxy`, ensure the proxy initializer middleware is not duplicated or misplaced."],"exampleFix":"// before — UseRouting missing or after proxy\nvar app = builder.Build();\napp.MapReverseProxy(); // no UseRouting — throws\napp.Run();\n// after — correct ordering\nvar app = builder.Build();\napp.UseRouting();\napp.UseEndpoints(endpoints =>\n{\n    endpoints.MapReverseProxy();\n});\napp.Run();","handlingStrategy":"validation","validationCode":"// Verify middleware ordering at startup\n// Ensure UseRouting is registered before the proxy endpoints\n// This is a code-structure check, not a runtime validation\n/*\nRequired order:\n  app.UseRouting();\n  app.UseEndpoints(endpoints => { endpoints.MapReverseProxy(); });\n*/","typeGuard":"// No type guard — this is a pipeline-configuration issue.","tryCatchPattern":"// Not applicable — this is a startup configuration error.\n// Fix by adding UseRouting() before the proxy endpoints.","preventionTips":["Always call app.UseRouting() before app.MapReverseProxy().","Use the standard Program.cs template from YARP documentation.","Review middleware ordering after refactoring Program.cs."],"tags":["pipeline","routing","middleware","endpoint","startup"],"backgroundTag":null,"analyzedSha":"bd11867bee7df522e7fd3effb08a9c85fd616908","analyzedAt":"2026-08-13T21:29:49.359Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}