У меня есть метод async
, который вызывает 3 разных API, чтобы получить некоторые данные, а затем отправить ответ на следующий API. Теперь на основании статьи Стивена Клири here избежать тупиковой ситуации:Использование ConfigureAwait (false) для частных методов async?
1 ваших «библиотека» асинхронные методы, использовать ConfigureAwait (ложь), где это возможно.
2. Не работает блок задач; используйте async полностью вниз.
Я хотел бы знать, верно ли то же самое для частных асинхронных методов? Нужно ли использовать ConfigureAwait(false)
, когда я вызываю метод асинхронизации private
? Так что-то вдоль линии
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 отправлять here, но ответить предлагая использовать пользовательские NoSynchronizationContextScope.
Я хотел знать, нужно ли мне использовать ConfigureAwait (false) для частных методов или нет?
Эталонная реализация: http://stackoverflow.com/questions/28305968/use-task-run-in-synchronous-method-to-avoid-deadlock-waiting-on-async-method – Caramiriel