MEF and ASP.NET services

Jan 4, 2011 at 8:22 PM


I try to use MEF to instantiate my data layer. This works perfectly fine in a console app, but not in a RIA services (SERVER SIDE).

The console app has code likes this:


            CompositionContainer _CompositionContainer = new CompositionContainer(new ConfigExportProvider());

        public CodeCamp.DataServerInterface.IDataServer DataServer { get; set; }


If I run the app I do have a DataServer instance, all is fine.


For the web app I added the CompositionContainer code to the apoplication start in gloabl asax. It gets called and the catalog contains an instance (Export) of the data server)

I have a service file which looks like below. The ctor gets called, but any method on that service the SpeakerContext is always NULL.

What is different in a web app to get the import done ?



    public class POCOSpeakerService : DomainService
        public POCOSpeakerService()


        IDataServer SpeakerContext { get; set; }



Jan 6, 2011 at 12:56 AM

Sorry for my newbie question, I can answer this myself now.

The service class is only instantiated when needed.  So basically I would need to call _CompositionContainer.ComposeParts(this) on the service ctor.

However,  this is expensive. Is there any better way of doing this ? I can use a static application wide member, but what if I indeed need separate instances of my data server ?

Is there a patteren for doing this efficient ?



Jan 7, 2011 at 1:52 AM

I asked the RIA Services team, and here's the answer I got:

there is a settable static factory on DomainService (DomainService.Factory). This enables you to define your own factory. Here’s a blog post demonstrating this : Basically you set up the factory on app startup in global.asax and the runtime will use it for creation.