I've been coding a library that contains the most of the basic funtions from data handling from online services to storage handling and I've been stuck in a method that should upload an image to a host that would return the path to the file.
The problem is that when I try to make the call, the app crahses due to stack overflow.
Here is the complete method:
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 );
}
}
The origin of the StackOverflowException was here:
httpMultipartContent.Add( httpMultipartContent );
I was adding the object to itself, which can be self explanatory. I've swapped the variables, this is what it should look like:
httpMultipartContent.Add( httpStreamContent );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With