This project has moved and is read-only. For the latest updates, please go here.

Simple start-to-end example for MEF2?

Aug 9, 2013 at 7:49 PM
I'm trying to eval MEF2 right now, but I'm having tons of problems figuring it out due to a number of factors (e.g., MEF2 is in flux, searches bring up MEF1 results, I'm dumb, etc).

My goals are exceedingly simple, so I'm hoping that someone who understands how it fits together can point me in the correct direction.

What I've got so far is a very simple console solution with two projects, one that hosts a plugin interface and one that implements it. The implementing project throws its built DLL into the bin folder of the plugin host, so that after building the assembly containing the DLL should be easily discoverable. The plugin host knows nothing about the implementer, other than any implementers are going to be accessible in the bin folder.

My goal is to do the following:
  1. Run the plugin host console application
  2. Spin up MEF2, telling it that I want types that implement IPlugin (original, I know)
  3. Get new instances of all types that implement IPlugin every time I ask MEF2 for it
  4. Have a beer, because MEF is doing all the heavy work for me, and I'm done
What I have done is create my two projects, and in the plugin host, and a bunch of MEF2 code that doesn't do anything.

So, given the following plugin defintion
    public interface IPlugin
        void Run();
and the given console program:
class Program
    static void Main(string[] args)
        var plugins = GetPluginsViaMef2LolKThx();
        foreach(var plugin in plugins)

    private static IEnumerable<IPlugin> GetPluginsViaMef2LolKThx()
        throw new NotImplementedException("HALP!");
could somebody do me a major solid and tell me how I can implement that method?
Aug 9, 2013 at 11:07 PM


I am assuming that you are using the version of MEF that comes with .Net 4.5. Here are a couple of things to get you started.

Let’s assume that you have two classes that implement IPlugin

[Export(typeof(IPlugin))] //Add these attributes to make them discoverable to MEF.

Plugin1: IPlugin In Plugin1.dll


Plugin2: IPlugin In Plugin2.dll

In your Main in hosting application, you need to define a DirectoryCatalog with the path where the binaries are dropped.

var directoryCatalog=new DirectoryCatalog(“<BINPATH>”)

var container=new CompositionContainer(directoryCatalog)

IEnumerable<IPlugin> plugins= container.GetExportedValues<IPlugin>(); // GetPluginsViaMef2LolKThx[This is the implementation of your method

plugins should have Plugin1 and Plugin2

In case you want your plugins to be completely oblivious to the use of MEF, you can use registration builder to define rules.

var builder=new RegistrationBuilder();


and pass that in to the DirectoryCatalog as follows.

var directoryCatalog=new DirectoryCatalog(“<BINPATH>”,builder)

hope that answers your question. (Excuse the email formatting)



Aug 13, 2013 at 7:59 PM
Appreciate the example. I was attempting to use the cut down version of MEF2 available via a nuget package

Now, that stripped down version doesn't have types such as DirectoryCatalog. So I think I understand why I was having such a hard time getting a hang of MEF2.

I suppose I'll just skip the NuGet version and go with the one in the 4.5 BCL.

Thanks again.
Aug 13, 2013 at 9:12 PM


for the nuget version of MEF you could use the method in ContainerConfiguration WithAssemblies( ) method.

You would need to pull the assemblies on your own in that case. The attributes should work the same way and the equivalent for registrationbuilder is conventionbuilder

hope that helps