I have been using MEF Preview 6 for Silverlight since the moment the came out, I have been really happy at that release, allows me to load XAP files dynamically in order to speed up the starting time of my Silverlight application as well as decompose my applications blocks to very maintainable pieces that will be downloaded on the clients machine on demand. So the love fest between me and MEF started on Preview 6.
MEF Preview 8 was release and I kept myself away from the download as I was still very happy to use Preview 6, fulfill all my needs. So why did I wanted to upgrade? Well, I had a few dlls that didn’t want to get into the package to get downloaded, so I thought that was time to upgrade to Preview 8 to see if there was any difference.
First let’s describe MEF for people that haven’t use this framework yet. The Managed Extensibility Framework will provide developers a way to load application extensions.
The structure of MEF is very simple to find out what extension to package.
Diagram from codeplex MEF
It’s time to write some code.
In preview 6 the way to get a package of assemblies was to create your Package Catalog
PackageCatalog catalog = new PackageCatalog();
and then added into a container
var container = new CompositionContainer(catalog);
So you can just download the package and attached it to your application:
Package.DownloadPackageAsync(new Uri(sXap, uriKind), (s, p) =>
if (LayoutRoot != null)
System.Collections.Generic.List<System.Reflection.Assembly> assemblies = p.Assemblies as System.Collections.Generic.List<System.Reflection.Assembly>;
System.Reflection.Assembly assembly = assemblies;
Now let’s talk about how Preview 8 changes all this:
So instead of PackageCatalog() you need to create an instance of
AssemblyCatalog catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
Silverlight 4 and MEF
With Silverlight 4 or .NET 4 for that matter, now all this has changed, you won’t need to download a that module to use it on your applications, MEF comes out of the box as well as WCF RIA Services, also known as .NET RIA Services.