GetAsync:不返回HttpResponseMessage [英] GetAsync : not returning HttpResponseMessage

查看:257
本文介绍了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 = "http://api.danubeco.com/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):

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

  1. 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):

  1. 在库"异步方法中,尽可能使用ConfigureAwait(false).
  2. 不要阻止任务;一直使用异步.

第二种解决方案建议您将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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆