Web UI上的.Result发生死锁 [英] Deadlock on .Result from Web UI

查看:127
本文介绍了Web UI上的.Result发生死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读以下主题 http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

并决定在我的库中编写一个通用的实用程序方法,以通过HTTPClient在远程URL上进行GET

and decided to write a common utility method in my library to do a GET on remote url via HTTPClient

public static async Task<T> GetAsync<T>(HttpGetObject getObject)
{
    string baseUrl = getObject.BaseUrl;
    string actionUrl = getObject.ActionRelativeUrl;
    string acceptType = getObject.AcceptType;

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(baseUrl);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptType));

        AddCustomHeadersToHttpClient(client, getObject);

        // HTTP GET
        HttpResponseMessage httpResponseMessage = await client.GetAsync(actionUrl).ConfigureAwait(false);
        if (httpResponseMessage.IsSuccessStatusCode)
        {
            T response = await httpResponseMessage.Content.ReadAsAsync<T>().ConfigureAwait(false);
            return response;
        }
        else
        {
            string message = httpResponseMessage.Content.ReadAsStringAsync().Result;
            throw new Exception(message);
        }
    }
    return default(T);
}

我知道"await httpResponseMessage.Content.ReadAsAsync().ConfigureAwait(false)"将防止上述代码中的死锁 第一的: 我的查询是针对字符串消息= httpResponseMessage.Content.ReadAsStringAsync().Result"行,.Result会导致死锁或不在该行中吗?

I know the "await httpResponseMessage.Content.ReadAsAsync().ConfigureAwait(false)" will prevent the deadlock in the above code First: My query is for "string message = httpResponseMessage.Content.ReadAsStringAsync().Result" line, will .Result can cause deadlock or not in that line?

第二: 如果我这样从用户界面调用该代码:

Second: If I call that code from UI like this:

public static object DoGet()
{
    // Build getObject
    var task = Utility.GetAsync(getObject);
    task.Wait();
    var response = task.Result;
    return response;
}

会导致死锁吗?

请注意,我知道要避免与async-await发生混乱,从UI到DAL的所有方法都必须是async-await,但目前我无法改变所有结构,这是我目前的目标是调用HttpClient库并执行一些GET操作.

Please note that I know to avoid all the mess with async-await, all the methods from UI to DAL must be async-await but I am not in position at this moment to change all that structure, my goal at this moment is to call HttpClient library and do a few GET operations.

所以我的问题是上面的代码会导致死锁吗?

So my questions is that will the above code can cause a deadlock?

第三:

是task.Wait();甚至上面的代码中需要吗?

Is task.Wait(); even needed in the above code?

推荐答案

在一般情况下,您应该假定,对上的任何内容调用.Result.Wait() awaitable是危险的,并且可能导致死锁(除非您是发出任务的库,并且您了解完整的上下文). 可能在某些特定情况下可以正常工作,但是即使今天 仍然可以,但您不应该不要依靠这种行为.

In the general case, you should assume that yes, calling .Result or .Wait() on anything awaitable is dangerous and can deadlock (unless you are the library issuing the task, and you understand the full context). It is possible that it will work OK in some specific cases, but you should not rely on that behaviour, even if it works today.

这篇关于Web UI上的.Result发生死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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