I have inherited a large web application that uses MVC5 and C #. Some of our controllers make several slow database calls, and I want to make them asynchronous to allow worker threads to serve other requests, waiting for the database calls to complete. I want to do this with the least amount of refactoring. Say I have the following controller
public string JsonData()
{
var a = this.servicelayer.getA();
var b = this.servicelayer.getB();
return SerializeObject(new {a, b});
}
I made two expensive calls a, b asynchronous, leaving the service level unchanged and rewriting the controller as
public async Task<string> JsonData()
{
var task1 = Task<something>.Run(() => this.servicelayer.getA());
var task2 = Task<somethingelse>.Run(() => this.servicelayer.getB());
await Task.WhenAll(task1, task2);
var a = await task1;
var b = await task2;
return SerializeObject(new {a, b});
}
- , , Visual Studio, Task.Run() asp.net , , . - - ? , , async . , , , async? , , , , . 2 ? - 2 , ?
user1625066