Search code examples
asp.net-coreiishttp-headerskestrel-http-serverresponse-headers

Is it possible to force return the Connection header from ASP.NET Core through IIS?


Note: This is similar to this question but is about IIS, not Azure App Service on Linux.

I have an API endpoint for uploading large files. The file should not be uploaded if authentication fails, if the filename is invalid, etc. We even accept a checksum and skip upload if the file already exists.

Unfortunately, Kestrel always drains the entire request body no matter what I do in user code (see this issue for details). In other words, the user has to "upload" the whole file before getting the response, even if my code does not read the response body and returns an early error response. In that issue I received a tip that I could return a Connection: close header to avoid that. Unfortunately, while I receive the header in the response when debugging locally, I do not receive it when running in production through IIS. In other words, IIS seems to remove that header if it's present in the ASP.NET Core response.

Is there any way to force return a Connection: close header through IIS?


Solution

  • I think you could try to disable allowKeepAlive for your asp.net core application.

    <system.webServer>
        <httpProtocol allowKeepAlive="false" />
    </system.webServer>
    

    enter image description here

    enter image description here