A coworker showed me some code in which they’re processing an uploaded file in ASP.NET Web Api. It was something like this:
Task<IEnumerable<HttpContent>> task = Request.Content.ReadAsMultipartAsync(provider);
return task.ContinueWith<HttpResponseMessage>(contents =>
{
//Do stuff...
}
What is the benefit of reading the file asynchronously?
Reading the file from the network socket is an I/O bound operation. Doing it asynchronously ensures that you are not jeopardizing worker threads on the server during this reading but you are taking advantage of an I/O Completion Port.