У меня есть этот асинхронный метод внутри 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> Post()
{
List result = new List();
if (Request.Content.IsMimeMultipartContent())
{
try
{
MultipartFormDataStreamProvider stream = new MultipartFormDataStreamProvider(HostingEnvironment.MapPath("~/USER-UPLOADS"));
IEnumerable bodyparts = await Request.Content.ReadAsMultipartAsync(stream);
IDictionary bodyPartFiles = stream.BodyPartFileNames;
IList newFiles = new List();
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;
}
. Мой вопрос: почему именно этот метод имеет возвращаемый тип Task? Не понятно "куда" или "кому" возвращает эту задачу? Как будто никто не ждет/получает возвращенный объект.
Интересно, какие будут последствия, если я верну вот так void:
РЕДАКТИРОВАТЬ:
Я пробовал приведенный ниже код, и он полностью ломает программу. это похоже на то, что среда выполнения теряет ссылку на код, сам код не завершает работу.
public async void Post()
{
List result = new List();
if (Request.Content.IsMimeMultipartContent())
{
try
{
MultipartFormDataStreamProvider stream = new MultipartFormDataStreamProvider(HostingEnvironment.MapPath("~/USER-UPLOADS"));
IEnumerable bodyparts = await Request.Content.ReadAsMultipartAsync(stream);
IDictionary bodyPartFiles = stream.BodyPartFileNames;
IList newFiles = new List();
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)
{
}
}
}