MEF and ASP.NET services

Jan 4, 2011 at 7:22 PM

Hi,

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());
            _CompositionContainer.ComposeParts(this);

        [System.ComponentModel.Composition.Import]
        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 ?

Thanks

Joe

    [EnableClientAccess()]
    public class POCOSpeakerService : DomainService
    {
        public POCOSpeakerService()
        {

        }

        [System.ComponentModel.Composition.Import]
        IDataServer SpeakerContext { get; set; }

 

 

Jan 5, 2011 at 11:56 PM

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 ?

 

Joe

Jan 7, 2011 at 12: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 : http://weblogs.asp.net/fredriknormen/archive/2009/11/28/wcf-ria-services-unity-domainservicefactory.aspx. Basically you set up the factory on app startup in global.asax and the runtime will use it for creation.