Home » MEF

From MEF Preview 5 to 6

4. August 2009 by Juliën Hanssens 0 Comments

It’s been a week or two now since MEF Preview 6 was released and we can see the team actually has listened carefully to the community with this update.

 

The Release Notes provide a decent place to start when upgrading Preview 5 to Preview 6 samples, so you really should read them. Most things you will probably encounter are renamed methods. Read the release notes and think logically and you’ll do fine in refactoring your project to the latest MEF release.

 

However, some aspects where neglected to be mentioned such as the metadata properties. The main thing is something that pretty much everyone with a custom ASP.NET MVC ControllerFactory (MefControllerFactory) shall encounter:

 

'System.Lazy<System.Web.Mvc.IController>' does not contain a definition for 'Metadata' and no extension method 'Metadata' accepting a first argument of type 'System.Lazy<System.Web.Mvc.IController>' could be found (are you missing a using directive or an assembly reference?)  

That sounds like a rename, all right. Besides the point that Export<T> has become Lazy<T> this means that the metadata property has been stripped. That… brought… complications.

 

Luckily, after some extensive digging I discovered that Nicholas Blumhardt posted the following:


The raw metadata dictionary is gone (if you truly require this, you can use the IDictionary<string, object> type as a metadata view interface)…

That means, in order for Hammett’s MEF and ASP.NET MVC sample to work you need to change

Export<IController> export = this.container.GetExports<IController>()
    .Where(c => c.Metadata.ContainsKey("ControllerName")
        && c.Metadata["ControllerName"].ToString() == controllerName)
        .FirstOrDefault();

… into the following sample (notice the IDictionary):

Lazy<IController> export = this.container.GetExports<IController, IDictionary<string, object>>()
    .Where(c => c.Metadata.ContainsKey("ControllerName")
        && c.Metadata["ControllerName"].ToString().ToLowerInvariant().Equals(controllerName.ToLowerInvariant())).
            FirstOrDefault();

And that’s about it.

Comments are closed