对专用异步方法使用ConfigureAwait(false)? [英] Using ConfigureAwait(false) for private async methods?

查看:211
本文介绍了对专用异步方法使用ConfigureAwait(false)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有公共的async方法,该方法调用3个不同的API来获取一些数据,然后将响应发布到下一个API.现在基于Stephen Cleary的文章此处至避免死锁:

I have public async method which calls 3 different APIs to get some data and then post the response to the next API. Now based on Stephen cleary's article here to avoid deadlock :

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

1.In your "library" async methods, use ConfigureAwait(false) wherever possible.
2.Don’t block on Tasks; use async all the way down.

我想知道私有异步方法是否同样如此?调用private异步方法时也需要使用ConfigureAwait(false)吗?沿途如此

I wanted to know if same is true for private async methods? Do I need to use ConfigureAwait(false) when I am calling private async method as well? So something along the line

public async Task<int> ProcessAsync(ServiceTaskArgument arg)
{
    // do i need to use ConfigureAwait here while calling private async method?
    var response1 = await GetAPI1().ConfigureAwait(false);

    // do i need to use ConfigureAwait here while calling private async method?
    var response2= await PostAPI2(response1).ConfigureAwait(false);

    // do i need to use ConfigureAwait here while calling private async method?
    await PostAPI3(response2).ConfigureAwait(false);

    return 1;
}

private async Task<string> GetAPI1()
{
    var httpResponse = await _httpClient.GetAsync("api1").ConfigureAwait(false);

    // do i need to use ConfigureAwait here while calling private async method?
    await EnsureHttpResponseIsOk(httpResponse).ConfigureAwait(false);

    return await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
}

private async Task<string> PostAPI2(string data)
{
    var stringContent = new StringContent(data, Encoding.UTF8, "application/json");
    var httpResponse = await _httpClient.PostAsync("api2", stringContent).ConfigureAwait(false);

    // do i need to use ConfigureAwait here while calling private async method?
    await EnsureHttpResponseIsOk(httpResponse).ConfigureAwait(false);

    return await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
}

private async Task<string> PostAPI3(string data)
{
    var stringContent = new StringContent(data, Encoding.UTF8, "application/json");
    var httpResponse = await _httpClient.PostAsync("api3", stringContent).ConfigureAwait(false);

    // do i need to use ConfigureAwait here while calling private async method?
    await EnsureHttpResponseIsOk(httpResponse).ConfigureAwait(false);

    return await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
}

private async Task EnsureHttpResponseIsOk(HttpResponseMessage httpResponse)
{
    if (!httpResponse.IsSuccessStatusCode)
    {
        var content = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
        throw new MyHttpClientException("Unexpected error has occurred while invoking http client.", content, httpResponse.Headers);
    }
}

更新1
我也曾在此处进行过讨论,但建议使用自定义 NoSynchronizationContextScope .
我想知道是否需要在私有方法上使用ConfigureAwait(false)?

Update1
Also I have gone through SO post here, but answer suggesting to use custom NoSynchronizationContextScope.
I wanted to know if I need to use ConfigureAwait(false) on private methods or not?

推荐答案

我想知道是否需要在私有方法上使用ConfigureAwait(false)?

I wanted to know if I need to use ConfigureAwait(false) on private methods or not?

通常,是的.除非方法需要其上下文,否则ConfigureAwait(false)应该用于每个 await.

As a general rule, yes. ConfigureAwait(false) should be used for every await unless the method needs its context.

但是,如果您使用NoSynchronizationContextScope,则无需使用ConfigureAwait(false).

However, if you use NoSynchronizationContextScope, then that removes the need for ConfigureAwait(false).

这篇关于对专用异步方法使用ConfigureAwait(false)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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