Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There is a pending asynchronous operation, and only one asynchronous operation can be pending concurrently

When running this on my dev machine no issues, works great. When deployed to my server I always get the error.

System.IO.IOException: Error reading MIME multipart body part. ---> System.InvalidOperationException: There is a pending asynchronous operation, and only one asynchronous operation can be pending concurrently. at System.Web.Hosting.IIS7WorkerRequest.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)

I am attempting to upload an image. When I run this on my local machine no errors and the file does get uploaded.

        public async Task<HttpResponseMessage> PostFile(int TaskID)
    {
        // Check if the request contains multipart/form-data. 
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        try
        {
            StringBuilder sb = new StringBuilder(); // Holds the response body 
            string root = HttpContext.Current.Server.MapPath("~/App_Data");
            var provider = new MultipartFormDataStreamProvider(root);

            //// Read the form data and return an async task. 
            await Request.Content.ReadAsMultipartAsync(provider);

The error hits the last line of the code that begins with await.

like image 702
David Kauffman Avatar asked Oct 20 '22 21:10

David Kauffman


1 Answers

Needed to add the following line of code.

Request.Content.LoadIntoBufferAsync().Wait();

This was added just before the line that was erroring out.

So it now looks like this.

            var provider = new MultipartFormDataStreamProvider(root);

            Request.Content.LoadIntoBufferAsync().Wait();

            //// Read the form data and return an async task. 
            await Request.Content.ReadAsMultipartAsync(provider);
like image 74
David Kauffman Avatar answered Nov 04 '22 18:11

David Kauffman