C# 等待 httpClient.SendAsync(httpContent) 无响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19704432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:40:49  来源:igfitidea点击:

await httpClient.SendAsync(httpContent) is non responsive

c#windows-phone

提问by Priti

await httpClient.SendAsync(httpContent)is not responding though I found no error in code/url its still getting hang. Please suggest/help.

await httpClient.SendAsync(httpContent)没有响应,虽然我发现代码/url 中没有错误,但它仍然挂起。请建议/帮助。

My code as follows:

我的代码如下:

public async Task<string> Get_API_Result_String(string url, List<KeyValuePair<string, string>> parameters)
{
    string res = "";

    try
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        //Prepare url
        Uri mainurl = new Uri(settings[FSAPARAM.UserSettingsParam.SERVERNAME].ToString());
        Uri requesturl = new Uri(mainurl, url);

        var httpClient = new HttpClient();
        var httpContent = new HttpRequestMessage(HttpMethod.Post, requesturl);
        // httpContent.Headers.ExpectContinue = false;

        httpContent.Content = new FormUrlEncodedContent(parameters);

        HttpResponseMessage response = await httpClient.SendAsync(httpContent);

        var result = await response.Content.ReadAsStringAsync();
        res = result.ToString();

        response.Dispose();
        httpClient.Dispose();
        httpContent.Dispose();
    }
    catch (Exception ex)
    {
        Logger l = new Logger();
        l.LogInfo("Get_API_Result_String: "+ url + ex.Message.ToString());
        ex = null;
        l = null;
    }

    return res;
}

Calling it in another class as follows:

在另一个类中调用它,如下所示:

NetUtil u = new NetUtil();
string result = await u.Get_API_Result_String(Register_API, values);
u = null;

回答by Stephen Cleary

I predict that further up your call stack, you are calling Waitor Resulton a returned Task. This will cause a deadlockthat I explain fully on my blog.

我预测在您的调用堆栈的更深处,您正在调用WaitResult在返回的Task. 这将导致我在我的博客上详细解释的僵局

To summarize, awaitwill capture a context and use that to resume the asyncmethod; on a UI application this is a UI thread. However, if the UI thread is blocked (in a call to Waitor Result), then that thread is not available to resume the asyncmethod.

总而言之,await将捕获上下文并使用它来恢复async方法;在 UI 应用程序上,这是一个 UI 线程。但是,如果 UI 线程被阻塞(在对Wait或的调用中Result),则该线程不可用于恢复该async方法。

回答by Guillermo Varini

this worked for me:

这对我有用:

httpClient.SendAsync(httpContent).ConfigureAwait(false);

回答by Sanjay Tank

I just removed awaitand just used as below and it worked:

我刚刚删除await并按如下方式使用,它起作用了:

var result = httpClient.SendAsync(httpContent).Result;

But that is not a good practice. As Nikolamentioned, we shouldn't mix sync and async calls.
I changed the calling method to async and problem got resolved.

但这不是一个好习惯。正如 Nikola提到的,我们不应该混合使用同步和异步调用。
我将调用方法更改为异步并解决了问题。

回答by Petar Zh.

This is OK on mine

这对我来说没问题

  var response = httpClient.SendAsync(request);
  var responseResult = response.Result;

  if (responseResult.IsSuccessStatusCode)
  {
         var result = responseResult.Content.ReadAsStringAsync().Result;
         return result;
  }