# Saturday, August 29, 2009

In Preview 6 of Microsoft's Managed Extensibility Framework (MEF), the framework changed the rules on matching multiple exports to a single import.

In previous versions of MEF, the attribute syntax was identical whether we were matching a single item or multiple items to an Import. Both scenarios used the [Import] attribute to tell MEF to find exports with a matching contract.

For example, if your application is using MEF to match a string variable, based on a string contract, you would use code similar to the following

string SomeString { get; set; }

This works if MEF finds exactly one matching export string, as in the following code.

string ThatExportedMefString
        return "This string was provided by an MEF contract.  It is from  an external assembly.";

If there is a chance MEF might find multiple Exports that satisfy the above contract, you would need (in previous versions) to modify the imported type, so that it implements IEnumerable, as in the following example

IEnumerable<string> SomeStringList { get; set; }

Beginning with MEF Preview 6, the rule for the attribute becomes more strict. If you are matching a number of items into an IEnumerable set of items on your import, you must replace the Import attribute with the ImportMany attribute. In the above example, the Import declaration becomes

IEnumerable<string> SomeStringList { get; set; }

The main advantage of this change is that ImportMany will not blow up if MEF finds no matching export for the contract. Import throws an exception if it cannot find a matching export.

Of course, your code will need to handle cases in which there are 0 matches, 1 match, or many matches when MEF seeks exports to match this contract. In the above example, that code might look like

foreach (string s in SomeStringList)

In my opinion, when you are writing an Import and you don't have control over the Export (for example, if you are allowing third-party vendors to supply the matching Exports), you should always use the ImportMany attribute. The only time you should use the Import attribute is if you are only looking for contract matches in assemblies that you have written and you can guarantee that there will always be exactly one match.

Tuesday, October 13, 2009 1:22:25 PM (GMT Daylight Time, UTC+01:00)

MEF has had such a rapid change that I'm not sure when this was introduced, but the zero or one case can be handled with a bit less code using the Import attribute with the AllowDefault parameter set to True. You then need to ensure null (zero, false or the beginning of time) will not explode your system, but it is a bit clearer on the zero or one intention and will fail if there are many and this indicates an issue in the system.

The other change since you wrote this is that the error no longer occurs with stable composition, but rather parts fail to be created.

Kathleen Dollard
Tuesday, July 26, 2011 1:53:34 PM (GMT Daylight Time, UTC+01:00)
Home run! Great slugging with that anwesr!
Wednesday, July 27, 2011 5:47:49 PM (GMT Daylight Time, UTC+01:00)
ZJOAcw , [url=http://wwdkdwbnxzvn.com/]wwdkdwbnxzvn[/url], [link=http://eqyrshnjxfzl.com/]eqyrshnjxfzl[/link], http://zgfffefdwjpg.com/
Saturday, July 30, 2011 2:07:54 PM (GMT Daylight Time, UTC+01:00)
6wpvlp , [url=http://fgcyogamcdbq.com/]fgcyogamcdbq[/url], [link=http://lzfuvxjezvrf.com/]lzfuvxjezvrf[/link], http://sivrmienluxp.com/
Comments are closed.