我想在Async方法中使用Func。我收到一個錯誤。使用Func委託與異步方法
無法將異步lambda表達式轉換爲委託類型
'Func<HttpResponseMesage>'
。異步lambda表達式可能返回void,Task或Task<T>
,其中沒有一個可轉換爲'Func<HttpResponseMesage>'
。下面
是我的代碼:
public async Task<HttpResponseMessage> CallAsyncMethod()
{
Console.WriteLine("Calling Youtube");
HttpClient client = new HttpClient();
var response = await client.GetAsync("https://www.youtube.com/watch?v=_OBlgSz8sSM");
Console.WriteLine("Got Response from youtube");
return response;
}
static void Main(string[] args)
{
Program p = new Program();
Task<HttpResponseMessage> myTask = p.CallAsyncMethod();
Func<HttpResponseMessage> myFun =async() => await myTask;
Console.ReadLine();
}
我有關於[異步委託類型](http://blog.stephencleary.com/2014/02/synchronous-and-asynchronous-delegate.html)的博客文章,您可能會發現有幫助。 –