我们如何将Task <httpresponsemessage>返回类型返回给HttpResponseMessage类型?

时间:2016-09-27 17:31:05

标签: c# asp.net-web-api

有没有办法可以将TaskHttpResponseMessage类型返回给HttpResponseMessage类型?下面是代码。

  private HttpResponseMessage Process(string receiver,HttpRequestMessage request)
       {
              Reciever test = new Reciever();
              Task<HttpResponseMessage> res= test.ReceiveAsync(receiver, request);
               return res;      
        }

实际上我的 ReceiveAsync 方法属于异步类型,我不希望进程方法为异步类型

对此有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:-1)

我认为这可行:

编辑:不要这样做,因为它会死锁 - 谢谢@Servy

private HttpResponseMessage Process(string receiver, HttpRequestMessage request)
{
    return ProcessAsync(receiver, request).Result;
}

private async Task<HttpResponseMessage> ProcessAsync(string receiver, HttpRequestMessage request)
{
    Reciever test = new Reciever();
    var  res = await test.ReceiveAsync(receiver, request);
    return res;
}