2015-11-14 95 views
0

我有一個類發佈到我的單元測試,但不是我的MVC控制器工作的API。Flurl.http PostJsonAsync消失

在測試儀中,以下代碼行很好,但是在MVC應用程序中調用時,我無法跨越以下行。

public async Task<string> post(object values)  
    { 
     using (var client = new HttpClient()) 
     { 
      try 
      { 
       var responseString = await address.PostJsonAsync(values).ReceiveString(); 
       return responseString; //Never reaches this line while in MVC. 

      } 
      catch (Exception e) 
      { 
       //Console.Error.WriteLine(e.Message); 
       return ""; 
      } 
     } 
    } 
+0

不確定我關注,我想這裏需要一些更多的細節。當你說你不能超越這條線時,它是否掛起?是否有某種異常拋出? –

+0

我已更新我的代碼段。在逐步完成代碼時,我將其設置爲PostJsonAsync,但從不返回返回語句的_either_。 – Aaron

回答

0

另一位同事幫我找到問題。它必須處理異步/等待/同步。 更改爲從呼叫中返回結果權限可解決問題。

public string post(object values) 
    { 
     try 
     { 
      var responseString = address.PostJsonAsync(values).ReceiveString().Result; 
      return responseString; 
     } 
     catch (Exception e) 
     { 
      //Console.Error.WriteLine(e.Message); 
      return ""; 
     }    
    } 
+0

你爲什麼要用'HttpClient'來使用程序塊?我無法發現它的任何用法,似乎是無關的。 –

+0

謝謝@MatthiasJansen我已經更新了答案。 – Aaron