线程安全和 MEF CompositionContainer
我计划在 ASP.NET 中使用 MEF,以深入了解 CompositionContainer 的线程安全性。
我的第一种方法将一个不同的 CompositionContainer 与每个请求相关联,但我担心这会很昂贵并且扩展性不好,另一方面 CompositionContainer 通过构造函数中的简单标志支持线程安全操作。
我还考虑了混合方法,我可能会使用线程安全的静态 CompositionContainer 和与每个请求绑定的容器。
除了线程安全论点之外,我还严重依赖 ExportFactory 来根据需要构造对象。尽管如此,我仍然被 ExportLifeTimeContext 的问题所困扰,并且我不确定这种方法的资源要求。
有人对此有所了解吗?
I'm planning on using MEF within ASP.NET looking for some insight into thread safety of the CompositionContainer.
My first approach associated a distinct CompositionContainer to each request but I'm worried that's gonna be expensive and not scale very well, on the other hand the CompositionContainer support thread safe operations through a simple flag in the constructor.
I've also considered the hybrid approach where I might use a thread-safe static CompositionContainer and one which is tied to each request.
Besides the thread safety argument I'm relying heavily on ExportFactory to be able to construct objects as needed. Though, I'm still bugged by this ExportLifeTimeContext thing and I'm uncertain about the resource requirements of this approach.
Anyone got some insight into this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建 CompositionContainers 的成本很低,因此为每个请求创建一个应该没问题。创建目录并不便宜,但目录是线程安全的,因此您应该能够在启动时创建一个全局目录并针对每个请求使用它。
需要注意的一件事是,即使是“线程安全”的组合容器对于可能导致重新组合的操作也不是线程安全的,例如更改目录或调用容器上的 Compose 方法。
至于混合方法,如果您希望在请求之间共享某些部分(应该是线程安全的)以及您希望特定于请求的某些部分,则可以采用该路线。在这种情况下,只需将共享容器创建为线程安全的。
Creating CompositionContainers is cheap so it should be fine to create one for each request. Creating the catalog is not as cheap, but the catalogs are thread-safe so you should be able to create a global one on startup and use that for each request.
One thing to be aware of is that even "thread-safe" composition containers aren't thread-safe for operations which can cause recomposition, such as changes to the catalogs, or calling the Compose methods on the container.
As for the hybrid approach, you would go that route if you have some parts (which should be thread-safe) that you want to be shared between requests and some parts that you want to be request-specific. In that case only the shared container would need to be created as thread-safe.