Why do I have to read the inputStream in the Global.asax Application_BeginRequest method passing to the controler?

0

I have a large body of JSON being sent to an endpoint of a .NET MVC app. The endpoint will always successfully execute the first time. The next time I use the endpoint, I get:

System.Web.HttpException (0x80004005): An error occurred while communicating with the remote host. The error code is 0x800703E5. ---> System.Runtime.InteropServices.COMException (0x800703E5): Overlapped I/O operation is in progress. (Exception from HRESULT: 0x800703E5)

If I reset IIS, the problem goes away for only 1 request. If I READ the inputStream in the Global.Asax.cs file, the problem goes away.

Here's the method signature in the controller:

public async Task<HttpResponseMessage> Post(int personId, int visitId, int documentId, [FromBody]StoredDocumentDetails value, string locale = null)

Here's the Global.asax.cs method:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var application = sender as HttpApplication;
        if (application != null && application.Context != null)
        {
            application.Context.Response.Headers.Remove("Server");
        }
        if (Log.IsDebugEnabled() && application != null)
        {

            var requestBytes = new byte[application.Request.InputStream.Length];
            var currentPosition = application.Request.InputStream.Position;
            application.Request.InputStream.Read(requestBytes, 0, requestBytes.Length);
            application.Request.InputStream.Position = currentPosition;
            var requestBody = Encoding.UTF8.GetString(requestBytes);
            Log.DebugFormat("Incoming Request Body: {0}", requestBody);
        }
    }
c#
asp.net
model-view-controller
inputstream
asked on Stack Overflow May 1, 2019 by Douglas Doughty • edited May 1, 2019 by Wai Ha Lee

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0