2016-01-06 64 views
3

我一直在編寫一個庫,其中包含從在線服務到存儲處理的數據處理的大部分基本功能,並且我一直堅持應該將圖像上傳到主機的方法會返回文件的路徑。HttpClient.SendRequestAsync觸發StackOverflow

問題是,當我嘗試撥打電話時,應用程序因堆棧溢出而死機。

下面是完整的方法:

public static async Task<WebResult> UploadImage(WebHost webHost, Object webPath, String webQuery, IRandomAccessStream imageStream) { 
    if (!AllowNetworkConnectivity) { 
     return new WebResult(false, true, null); 
    } 

    HttpClient 
     httpClient = new HttpClient(); 

    HttpMultipartFormDataContent 
     httpMultipartContent = new HttpMultipartFormDataContent(); 

    HttpStreamContent 
     httpStreamContent = new HttpStreamContent(imageStream); 

    httpStreamContent.Headers.Add("Content-Type", "application/octet-stream"); 
    httpMultipartContent.Add(httpMultipartContent); 

    try { 
     HttpRequestMessage 
      httpRequest = new HttpRequestMessage(HttpMethod.Post, new Uri(WebServerUrl(/* ! */))); 

     Dictionary<String, String> 
      webCredentialsDictionary = WebServerAuthenticationCredentials(webHost) as Dictionary<String, String>; 

     foreach (KeyValuePair<String, String> credentialEntry in webCredentialsDictionary) { 
      httpRequest.Headers.Add(credentialEntry); 
     } 

     httpRequest.Content = httpMultipartContent; 

     /* THE STACK OVERFLOW EXCEPTION IS TRIGGERED HERE */ 
     HttpResponseMessage 
      httpResponse = await httpClient.SendRequestAsync(httpRequest); 

     httpResponse.EnsureSuccessStatusCode(); 

     return new WebResult(true, true, null); 
    } catch (Exception exception) { 
     AppController.ReportException(exception, "Unable to upload image."); 

     return new WebResult(false, true, null); 
    } 
} 
+0

堆棧跟蹤是什麼樣的?原因*可能會進一步上升 –

+0

它似乎沒有任何問題。它只包含對此方法的調用,以及來自初始按鈕單擊事件的3個以及來自'mscorlib.ni'的一堆方法,但我認爲我看到的堆棧跟蹤是典型的移動應用。 – auhmaan

+1

'httpMultipartContent.Add(httpMultipartContent);'? – kennyzx

回答

0

的StackOverflowException的起源在這裏:

httpMultipartContent.Add(httpMultipartContent); 

我被添加到對象本身,它可以是自解釋的。我換了變量,這應該是這樣的:

httpMultipartContent.Add(httpStreamContent);