简体   繁体   English

GetAsync:不返回HttpResponseMessage

[英]GetAsync : not returning HttpResponseMessage

The apps should receive httpresponsemessage from LoginUser() but it becomes not responding. 应用程序应该从LoginUser()收到httpresponsemessage ,但它没有响应。

    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! 请帮忙!

You are blocking the UI thread and causing a deadlock. 您正在阻止UI线程并导致死锁。 From Stephen Cleary's blog (Just replace GetJsonAsync with your LoginUser method, and GetStringAsync with client.GetAsync ): 从Stephen Cleary的博客中 (只需用LoginUser方法替换GetJsonAsync ,并用client.GetAsync替换GetStringAsync ):

So this is what happens, starting with the top-level method (Button1_Click for UI / MyController.Get for ASP.NET): 因此,这是从顶级方法(UI的Button1_Click / ASP.NET的MyController.Get)开始发生的事情:

  1. The top-level method calls GetJsonAsync (within the UI/ASP.NET context). 顶级方法调用GetJsonAsync(在UI / ASP.NET上下文中)。

  2. GetJsonAsync starts the REST request by calling HttpClient.GetStringAsync (still within the context). GetJsonAsync通过调用HttpClient.GetStringAsync(仍在上下文中)来启动REST请求。

  3. GetStringAsync returns an uncompleted Task, indicating the REST request is not complete. GetStringAsync返回一个未完成的任务,指示REST请求未完成。

  4. GetJsonAsync awaits the Task returned by GetStringAsync. GetJsonAsync等待GetStringAsync返回的任务。 The context is captured and will be used to continue running the GetJsonAsync method later. 上下文被捕获,以后将用于继续运行GetJsonAsync方法。 GetJsonAsync returns an uncompleted Task, indicating that the GetJsonAsync method is not complete. GetJsonAsync返回一个未完成的Task,指示GetJsonAsync方法未完成。

  5. The top-level method synchronously blocks on the Task returned by GetJsonAsync. 顶级方法同步阻止GetJsonAsync返回的Task。 This blocks the context thread. 这将阻塞上下文线程。

  6. … Eventually, the REST request will complete. …最终,REST请求将完成。 This completes the Task that was returned by GetStringAsync. 这就完成了GetStringAsync返回的任务。

  7. 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的延续现在可以运行了,它等待上下文可用,以便可以在上下文中执行。

  8. 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. 顶级方法正在阻止上下文线程,等待GetJsonAsync完成,而GetJsonAsync正在等待上下文空闲以便可以完成。

And the simple available solutions (also from the blog): 和简单的可用解决方案(也来自博客):

  1. In your “library” async methods, use ConfigureAwait(false) wherever possible. 在“库”异步方法中,尽可能使用ConfigureAwait(false)。
  2. Don't block on Tasks; 不要阻塞任务; use async all the way down. 一直使用异步。

The 2nd solution suggests that you change button1_Click into: button1_Click解决方案建议您将button1_Click更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM