从WEB API Controller返回异步方法中的Void

时间:2012-08-11 13:33:04

标签: c# asp.net-mvc-4 asp.net-web-api async-await async-ctp

我从这个博客获得的ASP.NET MVC 4 WEB API Controller中有这个异步方法: http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/

  public async Task<IList<RecivedFile>> Post()
    {
        List<RecivedFile> result = new List<RecivedFile>();
        if (Request.Content.IsMimeMultipartContent())
        {
            try
            {
                MultipartFormDataStreamProvider stream = new MultipartFormDataStreamProvider(HostingEnvironment.MapPath("~/USER-UPLOADS"));

                IEnumerable<HttpContent> bodyparts = await Request.Content.ReadAsMultipartAsync(stream);
                IDictionary<string, string> bodyPartFiles = stream.BodyPartFileNames;
                IList<string> newFiles = new List<string>();

                foreach (var item in bodyPartFiles)
                {
                    var newName = string.Empty;
                    var file = new FileInfo(item.Value);

                    if (item.Key.Contains("\""))
                        newName = Path.Combine(file.Directory.ToString(), item.Key.Substring(1, item.Key.Length - 2));
                    else
                        newName = Path.Combine(file.Directory.ToString(), item.Key);

                    File.Move(file.FullName, newName);
                    newFiles.Add(newName);
                }

                var uploadedFiles = newFiles.Select(i =>
                {
                    var fi = new FileInfo(i);
                    return new RecivedFile(fi.Name, fi.FullName, fi.Length);
                }).ToList();

                result.AddRange(uploadedFiles);
            }
            catch (Exception e)
            {
            }
        }
        return result;
    }

我的问题是为什么这个方法确实有一个返回类型的任务?它还不清楚“在哪里”或“向谁”返回此任务?这就像没有人等待/接收返回的对象。

我想知道如果我像这样返回void会有什么影响:

修改

我已经尝试过以下代码,它完全破坏了程序。就像运行时失去对代码的引用一样,代码本身也没有完成运行。

    public async void Post()
    {
        List<RecivedFile> result = new List<RecivedFile>();
        if (Request.Content.IsMimeMultipartContent())
        {
            try
            {
                MultipartFormDataStreamProvider stream = new MultipartFormDataStreamProvider(HostingEnvironment.MapPath("~/USER-UPLOADS"));

                IEnumerable<HttpContent> bodyparts = await Request.Content.ReadAsMultipartAsync(stream);
                IDictionary<string, string> bodyPartFiles = stream.BodyPartFileNames;
                IList<string> newFiles = new List<string>();

                foreach (var item in bodyPartFiles)
                {
                    var newName = string.Empty;
                    var file = new FileInfo(item.Value);

                    if (item.Key.Contains("\""))
                        newName = Path.Combine(file.Directory.ToString(), item.Key.Substring(1, item.Key.Length - 2));
                    else
                        newName = Path.Combine(file.Directory.ToString(), item.Key);

                    File.Move(file.FullName, newName);
                    newFiles.Add(newName);
                }

            }
            catch (Exception e)
            {
            }
        }

    }

3 个答案:

答案 0 :(得分:7)

ASP.NET运行时等待它。您可能会发现this video有用。

async的大多数示例都假定UI上下文。 ASP.NET also provides a context,但它是一个“请求”上下文 - 每个HTTP请求一个。我的async/await post概述了这个“背景”,async/await FAQ详细介绍了这一点。

答案 1 :(得分:0)

如果返回类型为void,则不要等待对象返回..等待操作完成。等待任务完成。

答案 2 :(得分:0)

如果返回void,则无论异步操作的完成状态如何,您都将立即返回204“No Content”响应消息。这是在VoidResultConverter的帮助下完成的。

  

注意:在RC上,你会看到它返回200“OK”响应,但是   在RTM中,它将返回204“无内容”响应。