当不涉及真正的I/O调用时,是否需要在异步链的所有级别上使用ConfigureAwait(false)? [英] Is ConfigureAwait(false) required on all levels of the async chain when no real I/O call is involved?

查看:113
本文介绍了当不涉及真正的I/O调用时,是否需要在异步链的所有级别上使用ConfigureAwait(false)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Azure Document Db客户端SDK之上实现可重用的适配器类型的库.

Implementing a reusable adaptor type of library on top of Azure Document Db Client SDK.

该库不仅可以在ASP.NET Core Web服务中运行,还可以在命令行应用程序,ASP.NET Web Api等中运行.

The library can run anywhere, not only in an ASP.NET Core web service, but in a command line app, ASP.NET Web Api etc.

在此库中,所有方法都是异步的,它们只是抽象层,使使用Document Db客户端api更加容易.唯一真正的异步调用-I/O请求-实际上是由Document Db SDK中的api在最底层进行的.我上面写的任何代码都只是在内存数据转换,转换中,不涉及实际的I/O调用,但它们也都是异步的,因为最低层的Document Db api是异步的.

In this library all methods are async and they are simply layers of abstraction that makes it easier to use Document Db client api s. The only real async call - I/O request - is actually done on the lowest layer by the api s from the Document Db SDK. Any code above that I wrote is just in memory data transformations, conversions, no actual I/O call is involved but they are all async as well since the lowest layer Document Db api s are async.

我是否仍需要在堆栈中所有高层代码的所有层上使用ConfigureAwait(false),还是足以在我自己的调用Document Db SDK的方法的代码的最底层上调用ConfigureAwait(False)?真正的I/O调用?

Do I still need to use ConfigureAwait(false) on all layers of upper level code in the stack, or is that enough to only call ConfigureAwait(False) on the lowest layer of my own code that calls Document Db SDK s methods which make the real I/O call ?

推荐答案

与在await调用之前的同步代码不同,延续在不同的调用堆栈调用的上下文中执行.特别是,它们被安排为作为单独的委托执行,并且它们之间没有调用堆栈关系.因此,为最后一个连续执行指定ConfigureAwait(false)仅对此特定连续有效,其他连续将仍使用其各自的调度配置执行.也就是说,如果您的目标是确保不通过库中的任何延续来捕获同步上下文(以防止潜在的死锁或任何其他原因),则应配置所有以ConfigureAwait(false)延续的await调用.

The continuations unlike a synchronous code which precedes an await call, are executed in context of different call-stack calls. In particular they are scheduled for execution as separate delegates and there is no call-stack relationship between them. So specifying ConfigureAwait(false) for very last continuation execution will take effect only for this specific continuation, other continuations will still execute using their individual scheduling configuration. That is, if your goal is to ensure not capturing a synchronization context by any continuation in your library (in order to prevent potential dead locks or any other reason) then you should configure all await calls which have continuations with ConfigureAwait(false).

这篇关于当不涉及真正的I/O调用时,是否需要在异步链的所有级别上使用ConfigureAwait(false)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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