2

I have the sample code below in an ASP.NET MVC application. I am not able to get the data at all. In otherwords even though I am awaiting for the completion of the task Nothing happens and control never returns to the Index Action. What am I missing?

public class HomeController : Controller
    {
        //
        // GET: /Home/

        public async Task<ActionResult> Index()
        {
            Task<string> getContentsTask = GetContentsAsync();

            string contents = await getContentsTask;

            return Content("ssss");
        }

        async Task<string> GetContentsAsync()
        {
            int sample = 0;
            HttpClient client = new HttpClient();
            Task<string> contents = client.GetStringAsync("http://www.msdn.com");

            string data = await contents;
            return data;
        }

    }

Tried this in a console application and even in this the application exited as soon as i called the first await.

class Program
    {
        static void Main(string[] args)
        {
            GetContents();
        }
        public static async void GetContents()
        {
            Task<string> newTask = GetContentsAsync();
            string contents = await newTask;
            Console.WriteLine(contents);
            Console.ReadLine();
        }
        public static async Task<string> GetContentsAsync()
        {
            HttpClient client = new HttpClient();
            Task<string> getStringTask = client.GetStringAsync("http://www.msdn.com");
            string content = await getStringTask;
            return content;
        }
    }

When I give a Thread.Sleep(8000); then this is working fine. The calls are returning correctly.

Task<string> getContentsTask = GetContentsAsync();
Thread.Sleep(10000);
string contents = await getContentsTask;
Ry-
  • 218,210
  • 55
  • 464
  • 476
ckv
  • 10,539
  • 20
  • 100
  • 144
  • 1
    I presume your doing something else between the creation of the task and the await? Or else the whole thing's a bit pointless? – Liam Sep 30 '13 at 13:38
  • 2
    `Index()` returns a `Task`. So what does `return Content("ssss");` do? – Jeroen van Langen Sep 30 '13 at 13:39
  • I am just learning my way through async and await. The application does not do anything. – ckv Sep 30 '13 at 13:41
  • It works for me when I run it inside a console application, e.g. with the code `new HomeController().Index().Result`. Are you sure that the problem isn't something outside the code here? – Tim S. Sep 30 '13 at 13:48
  • There is no other piece of code. This is all there is. – ckv Sep 30 '13 at 13:52
  • 1
    @JeroenvanLangen his syntax is perfectly valid. `Content` returns something that extends `ActionResult`, and the `async` keyword automatically wraps it as a `Task` that implements the `await`(s) in the method. – Tim S. Sep 30 '13 at 13:53
  • `await` does a async wait. In the mean time the method returns a `Task` object. In this case the action method is returned immidiately after `await`. Which ends the HTTP Response. And after the response is sent your method call ends. But the thing is why do you need async-await in a web application? – Hossain Muctadir Sep 30 '13 at 13:58
  • 1
    Are you using MVC 3 or 4? – Tim S. Sep 30 '13 at 13:58
  • @MuctadirDinar It allows you to have the thread pool threads servicing other requests rather than tying them up just sitting there waiting on IO. The various web frameworks were all updated at the same time to provide mechanisms that support asynchronous web handlers so that the request *doesn't* end when the single blocking handler ends, but rather allows it to wait until all async operations end. – Servy Sep 30 '13 at 14:01
  • The problem here is HTTP cycle ends before `GetContentsAsync` method returns. – Hossain Muctadir Sep 30 '13 at 14:04
  • @MuctadirDinar If he's not using the newest framework version, yes. If he is, then it shouldn't be. – Servy Sep 30 '13 at 14:06
  • @ckv everything you have written is fine i have checked in for windows 8 app example..so maybe your error with the version you are using – loop Sep 30 '13 at 15:27

1 Answers1

0

Please ensure that you are using .NET 4.5 and that you have httpRuntime.targetFramework set to 4.5 in your app.config.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810