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方法?
感谢您的帮助。
答案 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
});
}