2016-01-06 34 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);