异步控制器行为

时间:2013-07-27 07:11:57

标签: c# .net asp.net-mvc asp.net-mvc-3

AsyncManager.Parameters["headlines"] = value;正在分配标题值。但是在调用headlines方法时,NewsCompleted将变为空。

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.)我该如何调试?

2.)并且当OutstandingOperations计数变为0时,框架通过从线程池请求新线程来调用我的actionCompleted方法。当调用这个actionCompleted方法时,我调查了调用堆栈,并且不知道背后究竟发生了什么。我的意思是框架如何知道应该使用AsyncManager中的此参数调用此特定的actionCompleted方法?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

尝试这些调整..

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
    });
}