WebAnalyticsService and MEF SL application with CompositionHost.Initialize

Jul 2, 2010 at 2:02 PM

I would like to use MSAF with one of our silverlight applications. Our application uses MEF to dynamically download and resolve assemblies:

var catalog = new AggregateCatalog();
catalog.Catalogs.Add(CreateCatalog("1.xap"));
catalog.Catalogs.Add(CreateCatalog("2.xap"));

CompositionHost.Initialize(new DeploymentCatalog(), catalog);

where

 

private CustomDeploymentCatalog CreateCatalog(string uri)
        {
            var catalog = new CustomDeploymentCatalog(uri);
            catalog.DownloadCompleted += DownloadCompleted;
            catalog.DownloadAsync();
            return catalog;
        }

 

We would like to use WebAnalyticsService defined in <Application.ApplicationLifetimeObjects> as well, however it causes the code above ( CompositionHost.Initialize() to be more precise) to raise an exception ( indicating that compositionhost has already been initialized ). I suppose it is caused by WebAnalyticsService using MEF to import some of its dependencies and initializing compositionhost implicitly

The workaround for this we used is a custom ApplicationService that is started before WebAnalyticsService (our service is defined in App.xaml's Application.ApplicationLifetimeObjects before WebAnalyticsService):

 

 
public void StartService(ApplicationServiceContext context) { 
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(CreateCatalog("1.xap"));
catalog.Catalogs.Add(CreateCatalog("2.xap"));

CompositionHost.Initialize(new DeploymentCatalog(), catalog);
}

 

 

But this solution is obviously hackish... do you have any suggestions indicating a neater solution :)? How should MSAF be used with MEF-enabled applications?