Reputation: 2512
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?
Upvotes: 2
Views: 1677
Reputation: 1039298
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.
Upvotes: 4
Reputation: 81680
It is all about freeing the thread perocessing the request by using async. Once you do that, the thread will be able to serve another request.
Also better not use ContinueWith
, I am explaining why.
Upvotes: 3
Reputation: 120508
Synchronous IO takes time and hangs your code, even if it's off the disk (as opposed to the net). Switching to async IO mitigates this by letting the framework deal with your IO requests without blocking your client code.
Upvotes: 1