有一个待处理的异步操作,并且只能有一个异步操作并发处理

Dav*_*man 5 c# multithreading asynchronous asp.net-web-api2

在我的开发机上运行此程序时,没有问题,效果很好。部署到我的服务器时,我总是会收到错误消息。

System.IO.IOException:读取MIME多部分正文部分时出错。---> System.InvalidOperationException:存在一个挂起的异步操作,并且只能同时挂起一个异步操作。在System.Web.Hosting.IIS7WorkerRequest.BeginRead(Byte []缓冲区,Int32偏移量,Int32计数,AsyncCallback回调,对象状态)

我正在尝试上传图片。当我在本地计算机上运行此文件时,没有错误,并且文件也已上传。

        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);
Run Code Online (Sandbox Code Playgroud)

错误到达以await开头的代码的最后一行。

Dav*_*man 3

需要添加以下代码行。

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

这是在出错的行之前添加的。

所以现在看起来像这样。

            var provider = new MultipartFormDataStreamProvider(root);

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

            //// Read the form data and return an async task. 
            await Request.Content.ReadAsMultipartAsync(provider);
Run Code Online (Sandbox Code Playgroud)