HttpClient.SendRequestAsync触发StackOverflow

时间:2016-01-06 13:24:38

标签: c# windows-phone-8

我一直在编写一个包含从在线服务到存储处理的数据处理的大部分基本功能的库,而且我一直困在一个应该将图像上传到主机的方法中返回文件的路径。

问题在于,当我尝试拨打电话时,应用程序会因堆栈溢出而崩溃。

这是完整的方法:

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 );
    }
}

1 个答案:

答案 0 :(得分:0)

StackOverflowException的原因在于:

type User struct {
    Name string
    IDs  map[string]string
}

func NewUser() *User {
   return &User{
       IDs: make(map[string]string),
   }
}

我正在将对象添加到自身,这可以是自我解释的。我已经交换了变量,它应该是这样的:

provider := "github" // could be facebook or google too
id := "12345"

user := NewUser()
user.Name = "Bob"
user.IDs[provider] = id