This project has moved and is read-only. For the latest updates, please go here.

MEF and WCF Service Instantiation

Jun 10, 2009 at 6:17 PM


I'm trying to design an application where there are several WCF services hosted in IIS and MEF is in charge of composing the services and their dependencies on *shared* components. I can't quite decide on where and at what times the composition should be performed.

Seeing as several service instances will share exported parts I gather that I should have a single CompositionContainer available to these services. My first question is where will this CompositionContainer reside? Do I put it in an IInstanceProvider, in a ServiceHost or at the highest level - the Application level? Considering that I need to share dependencies across endpoints and across service, am I forced to use the application level?

A second question is in what manner should each service have its imports fulfilled by MEF? Assuming the right time for each service to be composed is in its constructor, do I invoke CompositionContainer.Compose() there? Wouldn't that cause problems with any service instances already instantiated by the shared CompositionContainer?

Would using CompositionContainer.GetExportedObjects<T>() instead of Compose() be a better solution? Doesn't it take the point out of using MEF and having it match Import to Export by itself?