In my controller I need to call a method BlockingHttpRequest()
which makes an http request. It is relatively slow running, and blocks the thread.
I am not in a position to refactor that method to make it async.
Is it better to wrap this method in a Task.Run
to at least free up a UI/controller thread?
I'm not sure if this really improves anything, or just makes more work.
public class MyController : Controller
{
public async Task<PartialViewResult> Sync()
{
BlockingHttpRequest();
return PartialView();
}
public async Task<PartialViewResult> Async()
{
await Task.Run(() => BlockingHttpRequest());
return PartialView();
}
}
In practice I have a couple of situations like this, where BlockingHttpRequest()
takes 500ms and 5000ms respectively.
I understand that Task.Run()
will not make my function return sooner.
I thought that there might be some benefit of increased throughput. By freeing up the controller's thread, does that make it available to other users? This would imply that in MVC there is a difference between controller threads and background worker threads.