There is a pending asynchronous operation and only one asynchronous operation can be deferred at a time

No problem running this program on my computer, works fine. When deploying to my server, I always get an error.

System.IO.IOException: Error reading multiple-part MIME part. ---> System.InvalidOperationException: There is a pending asynchronous operation and only one asynchronous operation can be pending at a time. at System.Web.Hosting.IIS7WorkerRequest.BeginRead (byte [] buffer, Int32 offset, Int32 number, AsyncCallback callback, object state)

I am trying to upload an image. When I run on my local machine errors and the file is downloaded.

        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 code that starts with an wait.

+3


source to share


1 answer


You need to add the following line of code.

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

This was added just before the line that was in error.



So now it 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);

      

+1


source







All Articles