NikitaKo NikitaKo - 1 year ago 160
ASP.NET (C#) Question

Process multiple user requests with mvc async controller method

I have long-working sync controller method, and while it is running, I cannot get answers to other user requests.

I made everything async using async/await and ConfigureAwait(False), but I still need to wait.

Simple example:

public async Task<ActionResult> Index()
var result = await Task.Run(() => Wait()).ConfigureAwait(false);

return View(result);

private async Task<bool> Wait()
await Task.Delay(60000);

return true;

Now, if I send request to this method "mycontroller/Index", I expect that I can get View for another method "anotherController/Index", but I should wait until async method is completed.

Is it normal behavior? Should I use async to free ui thread and make it available until long-running method is completed?

Answer Source

ASP.NET serializes same-Session requests to avoid problems of simultaneous writes to the same Session from multiple threads. Actions of controllers marked with session ReadOnly attribute will be executed in parallel:

public class MyController : Controller
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download