簡體   English   中英

對專用異步方法使用ConfigureAwait(false)?

[英]Using ConfigureAwait(false) for private async methods?

我有一個公共的async方法,該方法調用3個不同的API來獲取一些數據,然后將響應發布到下一個API。 現在,根據斯蒂芬·克利里的文章, 在這里 ,以避免死鎖:

1.在“庫”異步方法中,盡可能使用ConfigureAwait(false)。
2,不要阻塞任務; 一直使用異步。

我想知道私有異步方法是否同樣如此? 在調用private異步方法時,是否還需要使用ConfigureAwait(false) 所以沿途有些東西

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

UPDATE1
我在這里也經過了SO發布,但是回答建議使用自定義NoSynchronizationContextScope
我想知道是否需要在私有方法上使用ConfigureAwait(false)?

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

通常,是的。 除非該方法需要其上下文,否則應將ConfigureAwait(false)用於每次 await

但是,如果使用NoSynchronizationContextScope ,則無需使用ConfigureAwait(false)

這取決於您的班級。 它是類庫的一部分嗎? 還是Web API或其他服務操作? 正如Stephen提到的, ConfigureAwait(false)避免死鎖是最安全的,但是如果您完全確定調用者沒有阻塞( .Wait().Result() ),它也可能添加很多不必要的代碼,例如,如果ProcessAsync是一個asp.net Web API方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM