{"record":{"id":"50484b7b31b78593","repo":"dotnet/yarp","slug":"current-request-can-t-be-delegated-either-the-req","errorCode":null,"errorMessage":"Current request can't be delegated. Either the request body has started to be read or the response has started to be sent.","messagePattern":"Current request can't be delegated\\. Either the request body has started to be read or the response has started to be sent\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ReverseProxy/Delegation/HttpSysDelegator.cs","lineNumber":67,"sourceCode":"            if (_queues.TryGetValue(key, out var queueWeakRef) && queueWeakRef.TryGetTarget(out var queue))\n            {\n                var detachedQueueState = queue.Detach();\n                Log.QueueReset(_logger, queueName, urlPrefix, detachedQueueState);\n            }\n        }\n    }\n\n    public void DelegateRequest(HttpContext context, DestinationState destination)\n    {\n        ArgumentNullException.ThrowIfNull(context);\n        ArgumentNullException.ThrowIfNull(destination);\n\n        var requestDelegationFeature = context.Features.Get<IHttpSysRequestDelegationFeature>()\n                    ?? throw new InvalidOperationException($\"{typeof(IHttpSysRequestDelegationFeature).FullName} is missing.\");\n\n        if (!requestDelegationFeature.CanDelegate)\n        {\n            throw new InvalidOperationException(\n                \"Current request can't be delegated. Either the request body has started to be read or the response has started to be sent.\");\n        }\n\n        if (_serverDelegationFeature is null || !_queuesPerDestination.TryGetValue(destination, out var queue))\n        {\n            Log.QueueNotFound(_logger, destination);\n            context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;\n            context.Features.Set<IForwarderErrorFeature>(new ForwarderErrorFeature(ForwarderError.NoAvailableDestinations, ex: null));\n            return;\n        }\n\n        Delegate(context, destination, _serverDelegationFeature, requestDelegationFeature, queue, _logger, shouldRetry: true);\n\n        static void Delegate(\n            HttpContext context,\n            DestinationState destination,\n            IServerDelegationFeature serverDelegationFeature,\n            IHttpSysRequestDelegationFeature requestDelegationFeature,","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/dotnet/yarp/blob/bd11867bee7df522e7fd3effb08a9c85fd616908/src/ReverseProxy/Delegation/HttpSysDelegator.cs#L49-L85","documentation":"Thrown by HttpSysDelegator.DelegateRequest when IHttpSysRequestDelegationFeature.CanDelegate is false. Http.sys delegation requires the request body not have been read and the response not have started; once bytes are sent or the body consumed, the connection can no longer be transparently handed off to a destination queue.","triggerScenarios":"Calling DelegateRequest after middleware has read from the request body stream, or after any response write/headers/HasStarted. The Http.sys feature refuses delegation in that state.","commonSituations":"A middleware earlier in the pipeline reads the request body (logging, buffering, auth). A response header or status is set before delegation. Response buffering middleware starts the response.","solutions":["Ensure delegation is decided before any request body reads or response writes.","Enable request body buffering (EnableBuffering) only if delegation is skipped, or move delegation before buffering.","Reorder middleware so HttpSysDelegator runs ahead of any component that touches the body/response."],"exampleFix":"// before — body read before delegation\nvar body = await new StreamReader(context.Request.Body).ReadToEndAsync();\n_delegator.DelegateRequest(context, destination); // CanDelegate == false\n// after — delegate first\n_delegator.DelegateRequest(context, destination);","handlingStrategy":"validation","validationCode":"var feat = context.Features.Get<IHttpSysRequestDelegationFeature>();\nif (feat is null || !feat.CanDelegate)\n{\n    context.Response.StatusCode = 503;\n    return;\n}","typeGuard":"static bool CanDelegateNow(HttpContext ctx) =>\n    ctx.Features.Get<IHttpSysRequestDelegationFeature>()?.CanDelegate ?? false;","tryCatchPattern":"try { _delegator.DelegateRequest(context, destination); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"can't be delegated\"))\n{ context.Response.StatusCode = 503; await context.Response.WriteAsync(\"Cannot delegate\"); }","preventionTips":["Do not read the request body or write the response before delegation.","Place delegation middleware early in the pipeline.","Check CanDelegate before calling DelegateRequest."],"tags":["httpsys","delegation","request-body","response-started","ordering"],"backgroundTag":null,"analyzedSha":"bd11867bee7df522e7fd3effb08a9c85fd616908","analyzedAt":"2026-08-13T21:29:49.359Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}