MEF, Silverlight and the DeploymentCatalog

I posted about the DeploymentCatalog in the new MEF preview the other day and I got a question or two along the lines of “show me an actual example rather than snippets” and so I thought I’d walk through a simple example here.

Let’s say that I want to build a Silverlight application that has a need for a photograph to be provided by a user and I don’t really care how this photograph is captured. It might be captured from the webcam, it might be captured from the file system or drag-drop or anything. I don’t mind, I just want to host a plugin that is happy to gather a photo from the user and then hand it to the rest of my code.

I started to write this up as a blog post but I realised it would involve so many steps that it’d take a long time so I quickly recorded it as a screen capture. Note that this is one of the roughest screen captures I’ve ever made as I largely just pressed the “record” button and then made stuff up on the fly but I hope it still captures the essence of things.

And here’s the code that I just zipped up and copied so it’s exactly as it was in the video – i.e. equally rough and ready.


In response to this post, a letter flooded in from my reader 🙂 Turns out he’s also called Mike ( hey Mike! ) and, honest, it’s not me writing to myself.

Mike’s point was around the idea of instancing. That is – I showed a little example of a Silverlight application dynamically adding a plug-in at runtime having downloaded it from the web but what if there was some dependency on another service that needed to be single-instanced?

Will that defeat the power of MEF?

In short, “no” 🙂 and the answer is buried in the Channel 9 MEF videos but given that I’d got the source-code kicking around I thought I’d just carry on the video…