Servers - Kestrel - Request draining

In this article

In this article, I'm going to show you how to reuse HTTP and HTTPS connections between your app and a server.

The draining process makes a tradeoff between allowing the connection to be reused and the time it takes to drain any remaining data:

Sometimes you may want to terminate the request immediately, before or after writing the response. For example, clients may have restrictive data caps. Limiting uploaded data might be a priority. In such cases to terminate a request, call HttpContext.Abort from a controller, Razor Page, or middleware.

There are caveats to calling Abort:

Calling HttpResponse.CompleteAsync before calling Abort ensures that the server has completed writing the response. However, client behavior isn't predictable and they may not read the response before the connection is aborted.

If there's any unread request body data after completing a response, then the server sends an HTTP/2 RST frame.

When sending a request to a server, it's a good idea to wait for the server to respond before sending the request body.

Ref: Request draining with ASP.NET Core Kestrel web server