GetAsync:不返回HttpResponseMessage

编程入门 行业动态 更新时间:2024-10-20 03:20:38
本文介绍了GetAsync:不返回HttpResponseMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

应用程序应该从 LoginUser()收到 httpresponsemessage ,但它没有响应.

The apps should receive httpresponsemessage from LoginUser() but it becomes not responding.

private void button1_Click(object sender, EventArgs e) { if (LoginUser(tUser.Text, Password.Text).Result.IsSuccessStatusCode) { Notifier.Notify("Successfully logged in.. Please wait!"); } else { Notifier.Notify("Please check your Credential.."); } }

public async Task<HttpResponseMessage> LoginUser(string userid, string password) { string URI = "api.danubeco/api/userapps/authenticate"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("c291cmF2OmtheWFs"); using (var response = await client.GetAsync(String.Format("{0}/{1}/{2}", URI, userid, password))) { return response; } } }

请帮助!

Please help!

推荐答案

您正在阻止UI线程并导致死锁. 摘自Stephen Cleary的博客(只需替换使用LoginUser方法,而GetStringAsync使用client.GetAsync):

You are blocking the UI thread and causing a deadlock. From Stephen Cleary's blog (Just replace GetJsonAsync with your LoginUser method, and GetStringAsync with client.GetAsync):

这就是发生的事情,从顶级方法开始 (用于UI的Button1_Click/用于ASP.NET的MyController.Get):

So this is what happens, starting with the top-level method (Button1_Click for UI / MyController.Get for ASP.NET):

  • 顶级方法调用GetJsonAsync(在UI/ASP.NET上下文中).

  • The top-level method calls GetJsonAsync (within the UI/ASP.NET context).

    GetJsonAsync通过调用HttpClient.GetStringAsync(仍在上下文中)来启动REST请求.

    GetJsonAsync starts the REST request by calling HttpClient.GetStringAsync (still within the context).

    GetStringAsync返回未完成的任务,表明REST请求未完成.

    GetStringAsync returns an uncompleted Task, indicating the REST request is not complete.

    GetJsonAsync等待GetStringAsync返回的任务.上下文被捕获,将用于继续运行 稍后使用GetJsonAsync方法. GetJsonAsync返回未完成的任务, 指示GetJsonAsync方法不完整.

    GetJsonAsync awaits the Task returned by GetStringAsync. The context is captured and will be used to continue running the GetJsonAsync method later. GetJsonAsync returns an uncompleted Task, indicating that the GetJsonAsync method is not complete.

    顶级方法同步阻止GetJsonAsync返回的Task.这会阻塞上下文线程.

    The top-level method synchronously blocks on the Task returned by GetJsonAsync. This blocks the context thread.

    ...最终,REST请求将完成.这样就完成了GetStringAsync返回的任务.

    … Eventually, the REST request will complete. This completes the Task that was returned by GetStringAsync.

    GetJsonAsync的延续现在可以运行了,它等待上下文可用,以便可以在上下文中执行.

    The continuation for GetJsonAsync is now ready to run, and it waits for the context to be available so it can execute in the context.

    死锁.顶级方法正在阻止上下文线程,等待GetJsonAsync完成,并且GetJsonAsync正在等待 上下文是免费的,以便可以完成.

    Deadlock. The top-level method is blocking the context thread, waiting for GetJsonAsync to complete, and GetJsonAsync is waiting for the context to be free so it can complete.

    以及简单的可用解决方案(同样来自博客):

    And the simple available solutions (also from the blog):

  • 在库"异步方法中,尽可能使用ConfigureAwait(false).
  • 不要阻止任务;一直使用异步.
  • 第二种解决方案建议您将button1_Click更改为:

    The 2nd solution suggests that you change button1_Click into:

    private async void button1_Click(object sender, EventArgs e) { if ((await LoginUser(tUser.Text, Password.Text)).IsSuccessStatusCode) { Notifier.Notify("Successfully logged in.. Please wait!"); } else { Notifier.Notify("Please check your Credential.."); } }

    更多推荐

    GetAsync:不返回HttpResponseMessage

    本文发布于:2023-11-17 12:16:25,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1609973.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:GetAsync   HttpResponseMessage

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!