Mef2P4 MVC: Adding custom ExportProvider(s)

Nov 22, 2011 at 3:09 PM

The CompositionProvider has methods to allow additional assemblies to be added and to allow a custom catalog to be specified if, for example, you wish to override the default conventions in the MvcApplicationCatalog class. In my application I have a custom ExportProvider I have been using to allow importing connectionstrings and appsettings from the application config file that I add to the CompositionContainer when I initialize it.

I have been looking at converting an application to use the latest Mef2P4 code drop with its new CompositionProvider , but can't see a way of including my custom ExportProvider to the it. Is this missing functionality that needs to be added in a future version by, say, having a method on the composition provider to add export providers, so that it can include them when it internally creates the CompositionContainer. Or is there an alternate mechanism that I should be looking at?

thanks,

Philip

Nov 22, 2011 at 6:50 PM

Hi Philip,

There are a couple of alternatives, but probably none that give the exact same behavior as you would currently get from your ExportProvider.

First, you could look at creating property exports on one or more ConnectionStrings parts. The property getters would access Web.config. This works so long as you know in advance how many connection strings you need to expose.

Second, you could create an IConnectionStringProvider service and implement that on a component, though it is a non-declarative approach.

Third, instead of a custom ExportProvider, you could create a custom ComposablePartCatalog. This is a bit trickier to do than creating an ExportProvider, but would enable the same functionality.

Catalogs are generally more appropriate than ExportProviders for most of these tasks, so we're hesitant to add an extension point for EPs in the MVC integration just yet. If you want some help converting to a catalog to implement the third approach we should be able to dig up some examples to guide you.

Regards,

Nick

Nov 22, 2011 at 9:42 PM

Thanks.

The third option of a custom ComposablePartCatalog for AppSettings and ConnectionStrings is the approach I had started to look at, so that is reassuring that it is a reaonable, and possibly a better mechanism in the long run.

After a little searching on the web, I have found a couple of examples that illustrate how to create such a custom ComposablePartCatalog, and have created a quick first go based on these that seems to have the right behaviour in my application. I basically included the following code in the Application_Start() method to initialize and aggregate the different catalogs like this:

var catalog = new MvcApplicationCatalog( new Assembly[]{ GetType().BaseType.Assembly,
                                                         typeof( MyServiceLib ).Assembly } );
var configCatalog = new ConfigurationCatalog();
var connectionCatalog = new ConnectionStringCatalog();

var catalogs = new AggregateCatalog( catalog, configCatalog, connectionCatalog );

CompositionProvider.SetCatalog( catalogs );

 

I still have to work out how to share the resulting configuration with a WCF service hosted in the same application, as either a web page or the WCF service can be the first "call" that initializes the application.  If it is the WCF service that is called, then the  Application_Start() method is not called so I will have to verify I can use the example code above when this is the case, but at this stage it all looks like it should be fine.

cheers,

Philip

Jan 14, 2012 at 9:44 PM
Edited Jan 14, 2012 at 9:52 PM

I am using the FactoryExportProvider from MefContrib that supports object factories, as I need to have control over the construction of particular types. 

How can I use or replace this object factory functionality in Mef2P5 MVC?

If there is not a simple means, I would vote for a way to add export providers via the new CompositionProvider. Something like:

 

static public void SetCatalog(ComposablePartCatalog catalog, params ExportProvider[] providers)

or

public static void AddExportProviders(params ExportProvider[] providers)

Thanks,

Richard

Jan 23, 2012 at 11:07 PM

Hi Richard,

Thanks for letting us know about this gap, we'll look into it in the time frame of our next preview.

Regards,

Nick