在Func <T>中封装异步方法

问题描述

首先,我希望标题可以。

我有一个带有知名问候服务的Grpc客户端,并尝试将SayHelloAsync调用封装在Func中。 但是=>运算符用红线表示,因为他不能转换他说的表达式。 如果我删除任何异步/等待,它会工作。 这可能是因为SayHelloAsync返回AsyncUnaryCall(有一个等待者)而不是Task,但最后它 我想这不是Grpc的问题,这只是我的误会。

这是我的尝试:

static async Task Main()
{
   var client = CreateGreeterClient();
   await UnsafeTask<Service.HelloReply>(async ()  =>
   {
       var result = await client.SayHelloAsync(new Service.HelloRequest { Name = greeter.Name });
       return Task<Service.HelloReply>.Fromresult(result);
   });
   Console.ReadKey();
}

static async Task<T> UnsafeTask<T>(Func<AsyncUnaryCall<T>> method) where T:class
{
   try
   {
      return await method();
   }
   catch (Exception exception)
   {
      Console.WriteLine(exception.Message);
      return null;
   }           
} 

任何提示都会很棒。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)