2013-07-27 167 views
1

AsyncManager.Parameters["headlines"] = value;被分配的頭條新聞價值。但是,當NewsCompleted方法被調用headlines都變得無效。異步控制器行爲

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方法被調用時,我查看了調用堆棧,並不知道背後究竟發生了什麼。我的意思是框架如何知道應該調用這個特殊的actionCompleted方法與AsyncManager這個參數?

感謝您的幫助。

回答

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

它爲我工作的罰款。但爲什麼我應該從字符串[]更改爲對象以使其工作? –

+0

AsyncManager.Parameters是一個的IDictionary <串,對象>。當你的項目添加到這個,它被添加爲傳遞的字符串鍵的對象。 –

+0

如果您正在使用MVC 4(框架4.5),你可以使用新的異步等待結構,這將使得控制器代碼更乾淨多了。 –