Csharp/C#教程:使用ConfigureAwait(false)进行私有异步方法?分享


使用ConfigureAwait(false)进行私有异步方法?

我有公共async方法,它调用3个不同的API来获取一些数据,然后将响应发布到下一个API。 现在根据Stephen cleary的文章来避免僵局:

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

我想知道私有异步方法是否也是如此? 我在调用private异步方法时是否需要使用ConfigureAwait(false) ? 所以沿途就行了

 public async Task 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 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 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 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
此外,我在这里发布了SOpost,但回答建议使用自定义NoSynchronizationContextScope 。
我想知道我是否需要在私有方法上使用ConfigureAwait(false)?

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

作为一般规则,是的。 除非方法需要其上下文,否则应该为每个 await使用ConfigureAwait(false)

但是,如果使用NoSynchronizationContextScope ,则不需要ConfigureAwait(false)

这取决于你的课程。 它是类库的一部分吗? 或者它是web api还是其他服务操作? 正如Stephen提到的,最安全的是ConfigureAwait(false)以避免死锁,但是如果你绝对确定调用者没有阻塞( .Wait().Result() ),它也可以添加许多不必要的代码,例如,如果ProcessAsync是一个asp.net web api方法。

上述就是C#学习教程:使用ConfigureAwait(false)进行私有异步方法?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1000462.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐