开发者

Thread safety and the MEF CompositionContainer

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 CompositionContain开发者_开发问答er 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜