1

AsyncManager.Parameters["headlines"] = value; is assigning headlines value. But headlines are become null when NewsCompleted Method is being called.

public void NewsAsync(string city)
    {
        AsyncManager.OutstandingOperations.Increment();
        NewsService newsService = new NewsService();
        newsService.GetHeadlinesCompleted += (value) =>
        {
            AsyncManager.Parameters["headlines"] = value;
            AsyncManager.OutstandingOperations.Decrement();
        };
        newsService.GetHeadlinesAsync(city);
    }

    public ActionResult NewsCompleted(string[] headlines)
    {
        return View("News", new ViewStringModel
        {
            NewsHeadlines = headlines
        });
    }

1.)How can i debug this?

2.)And also when OutstandingOperations count become 0, framework calls my actionCompleted method, by requesting new thread from thread pool. When this actionCompleted method is called, I looked into call stack and have no idea that what is exactly happening behind. I mean how the framework knows it should call this particular actionCompleted method with this parameter from AsyncManager?

Thanks for the help.

1 Answer 1

1

Try these tweaks..

public void NewsAsync(string city)
{
    AsyncManager.OutstandingOperations.Increment();
    NewsService newsService = new NewsService();
    newsService.GetHeadlinesCompleted += (sender, args) =>
    {
        AsyncManager.Parameters["headlines"] = args.Result;
        AsyncManager.OutstandingOperations.Decrement();
    };
    newsService.GetHeadlinesAsync(city);
}

public ActionResult NewsCompleted(object headlines)
{
    return View("News", new ViewStringModel
    {
        NewsHeadlines = (string[])headlines
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Its working fine for me. But why should I change from string[] to object to make it work?
AsyncManager.Parameters is a IDictionary<string, Object>. When you items add to this, it gets added as object for the string key passed.
If you are using mvc 4 (framework 4.5), you can use the new async await constructs, which will make the controller code much much cleaner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.