我想在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)的博客文章,您可能会发现有帮助。 –