admin管理员组文章数量:1393380
In Disposer.cs L80 (this one normally completes synchronously hence it has no impact) and L90 (this one impacts the situation below) you are using .ConfigureAwait(false)
which runs the continuations on the thread pool and not on the SynchronizationContext.
This leads to the behavior, that all subsequent Dispose
and DisposeAsync
calls after the first DisposeAsync
(which runs on the SynchronizationContext) run on worker threads and not on the thread which called Container.DisposeAsync
.
In the synchronous case, Container.Dispose
will call all Dispose
methods on the same thread where it was invoked.
The unexpected thread change during disposal leads to the problem that thread-sensitive Dispose
methods (e.g. accessing UI elements running on a UI thread) get into trouble and must take care of threading by their own.
I could not figure out from the docs what the intended threading model is and what to expect/assume, when calling Container.DisposeAsync
.
Getting rid of .ConfigureAwait(false)
in L80 and L90 would ensure that each Dispose
and DisposeAsync
call runs on the respective SynchronizationContext on which Container.DisposeAsync
is called.
Is this the intended behavior by AutoFac? If so, why is the .ConfigureAwait(false)
necessary? If it is necessary, how can I deal with these 'thread-changing' situation during dispose.
Thanks for any insights for this issue.
In Disposer.cs L80 (this one normally completes synchronously hence it has no impact) and L90 (this one impacts the situation below) you are using .ConfigureAwait(false)
which runs the continuations on the thread pool and not on the SynchronizationContext.
This leads to the behavior, that all subsequent Dispose
and DisposeAsync
calls after the first DisposeAsync
(which runs on the SynchronizationContext) run on worker threads and not on the thread which called Container.DisposeAsync
.
In the synchronous case, Container.Dispose
will call all Dispose
methods on the same thread where it was invoked.
The unexpected thread change during disposal leads to the problem that thread-sensitive Dispose
methods (e.g. accessing UI elements running on a UI thread) get into trouble and must take care of threading by their own.
I could not figure out from the docs what the intended threading model is and what to expect/assume, when calling Container.DisposeAsync
.
Getting rid of .ConfigureAwait(false)
in L80 and L90 would ensure that each Dispose
and DisposeAsync
call runs on the respective SynchronizationContext on which Container.DisposeAsync
is called.
Is this the intended behavior by AutoFac? If so, why is the .ConfigureAwait(false)
necessary? If it is necessary, how can I deal with these 'thread-changing' situation during dispose.
Thanks for any insights for this issue.
Share Improve this question asked Mar 13 at 12:53 Bernd StoetzelBernd Stoetzel 11 bronze badge1 Answer
Reset to default 0Looking at the Microsoft.Extensions.DependencyInjection disposal mechanism, it appears they, too, do ConfigureAwait(false)
for async disposables not using ValueTask
. That's admittedly a miss in Autofac, but it's consistent.
While I think Autofac could improve with some support for ValueTask
, the consistent behavior here is valuable to ensure folks switching from one container to another won't be surprised.
We'd be happy to take a PR to make Autofac ValueTask
and async disposable handling even closer to the Microsoft container, but to ensure consistent behavior, we likely won't switch how ConfigureAwait
works.
本文标签:
版权声明:本文标题:multithreading - Calling Autofacs Container.DisposeAsync* runs *DisposeDisposeAsync* methods of registered components on arbitra 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744699634a2620488.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论