tags:

views:

30

answers:

1

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?

+2  A: 

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.

Daniel Plaisted
That settles it, the catalog is only built once during startup. No recomposition. And I'll have a distinct container per request to go with that. Should work. Thanks!
John Leidegren