I have async controller with async action. In the action I call WCF service method in SomeMethodOne (it needs 10 seconds to return result) and then I execute some mathematical operations in SomeMethodTwo (it executes about 6 seconds on my computer). As I understand during waiting on result from WCF service method, my computer should execute SomeMethodTwo but it doesn't and all code executes 10 seconds + 6 seconds = 16 seconds. Why?
public class TestController : AsyncController
{
public async Task<ActionResult> Index()
{
string result = await SomeMethodOne();
SomeMethodTwo();
return View();
}
private async Task<string> SomeMethodOne() // it needs 10 seconds to return result from WCF service
{
using (Service1Client client = new Service1Client())
{
return await client.GetDataAsync(5);
}
}
private void SomeMethodTwo() // it executes about 6 seconds on my computer
{
double result = 0;
for (int i = 0; i < 1000000000; i++)
{
result += Math.Sqrt(i);
}
}
}
The WCF service which I run locally:
public class Service1 : IService1
{
public string GetData(int value)
{
Thread.Sleep(10000);
return string.Format("You entered: {0}", value);
}
}
AsyncControlleris deprecated. You only need to useasync Task<string>on a method to make it asynchronous.