Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Void in Async method from WEB API Controller

I have this async method inside ASP.NET MVC 4 WEB API Controller that I got from this blog: 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;
    }

My question is why exactly does this method have a return type of Task? It is not clear "where to" or "to whom" it returns this task? It's like there is no one that waits for/receives the returned object.

I wonder what will be the implications if I return void like this:

EDIT:

I have tried the code below and it completely breaks the program. it's like the runtime looses the reference to the code, the code itself doesn't finish running.

    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)
            {
            }
        }

    }
like image 711
user1561202 Avatar asked Aug 11 '12 13:08

user1561202


People also ask

Can we return void in async method?

In short, if your async method is an event handler or a callback, it's ok to return void .

How do I return a task in async?

If your async method needs to return int you'd mark the return type of the method as Task<int> and you'll return plain int not the Task<int> . Compiler will convert the int to Task<int> for you. public async Task MethodName() { return;//This should work but return is redundant and also method is useless. }

Is an async method that returns task a return keyword?

DoSomething()' is an async method that returns 'Task', a return keyword must not be followed by an object expression.


1 Answers

The ASP.NET runtime waits for it. You may find this video useful.

Most examples for async assume a UI context. ASP.NET also provides a context, but it's a "request" context - one for each HTTP request. My async/await post gives an overview of this "context", and the async/await FAQ goes into much more detail.

like image 105
Stephen Cleary Avatar answered Oct 11 '22 01:10

Stephen Cleary