您需要裝飾包含await
調用該方法async
和包裹它的返回類型Task<TResult>
。例如,如果你在一個MVC的Controller
一個Index()
行動調用它,你將不得不修改:
public ActionResult Index()
{
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, theContent);
//will give you the error you are getting
}
..所以就變成:
public async Task<ActionResult> Index()
{
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, theContent);
//no error here
}
對看一看async (C# Reference) MSDN。
當沒有返回有意義的值時,還有返回Task
的情況,void
主要用於定義需要返回類型的事件處理程序。
對於所有可能的返回類型async
方法,請參閱MSDN上的Async Return Types (C# and Visual Basic)。
另外,這些操作需要在項目中引用System.Threading.Tasks
。
要了解'async-await',請閱讀[my'async-await'curation](http://curah.microsoft.com/45553/asyncawait-general)上的文章。 –