我正在尝试将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();
}